diff --git a/evidence/ck_gates_cedc00181c311732b302fadcd2412601756920060c71fa30472a0d6caf30d74e.json b/evidence/ck_gates_cedc00181c311732b302fadcd2412601756920060c71fa30472a0d6caf30d74e.json new file mode 100644 index 00000000..f81f7456 --- /dev/null +++ b/evidence/ck_gates_cedc00181c311732b302fadcd2412601756920060c71fa30472a0d6caf30d74e.json @@ -0,0 +1,39 @@ +{ + "schema": "aienos.ck_gates.v1", + "started_utc": "2026-10-01T09:11:59Z", + "finished_utc": "2026-10-01T09:13:23Z", + "commit": "5124bec5d76be5214caeb721e35364faff65792a", + "commit_subject": "ck: NVMe shutdown review fixes (CFS, RDY, already-shut, EN=0 fallback)", + "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": 6, "log_sha256": "8a63ca4326bf4c13993166ef5355cca6c9835f42e964658ad5ea8117c63aed49", "log_lines": 21}, + {"name": "store", "script": "scripts/qemu_ck_store_test.sh", "present": true, "script_sha256": "b005ed29619d1113536a0686cedafe84fcea95fa30321307ec1d1335ea231925", "exit": "0", "seconds": 78, "log_sha256": "9b428c6880668f89c5d5e9ea0e7a44d918dd168e16bc5481c2b5657bce47e90d", "log_lines": 593} + ], + "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": "NVME_SHUTDOWN", "marker": "AIENOS_CK_NVME_SHUTDOWN", "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": 13, "pass": 6, "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 f83af2b2..9a49808b 100644 --- a/native/kernel/GATES.md +++ b/native/kernel/GATES.md @@ -19,8 +19,8 @@ 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` -`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 +`SMMU`, `NVME_SHUTDOWN` (from `scripts/qemu_ck_store_test.sh`; rows 21-24b, 47-72, 76 and +96-101 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`. @@ -231,17 +231,20 @@ complete. | 94 | corrupted binary rejected before `kernel: alive` | (none) | NOT_RUN (MISSING_IMPLEMENTATION: no CK corruption test) | | 95 | Secure Boot: signed boots, unsigned / tampered / wrong-key refused (Access Denied) | (none) | NOT_RUN (MISSING_IMPLEMENTATION: CK BOOTAA64.EFI never signed or booted under Secure Boot) | -## C-only gate +## C-only gates | # | C check | CK gate | Status | | --- | --- | --- | --- | | 96 | ARGUS-1 narrow revoke in-kernel (`argus: ok narrow_revoke=1 revoked=denied unrelated=granted authority=unchanged$`) | ARGUS1_REVOKE | DIFFERS (no Rust QEMU gate exists for it; checked on all 5 boots of both geometries, with `caps: ok granted=yes attenuated=yes amplify=denied forged=denied revoked=denied office_token=rndr$`) | | 97 | corrupt Store superblock (XOR 0xa5 over 2 x 4096 bytes) refused, disk untouched | M4_STORE | DIFFERS (C-only, boot 4: `store: REFUSED proof=structural `, `disk left as found, not reformatted`, `stage store: FAIL`, absent `store: committed`, image sha256 unchanged; nearest Rust check is row 79) | | 98 | no disk without DMA: Store refuses | M4_STORE | DIFFERS (C-only, boot 5 safe image: `store: REFUSED proof=io step="no boot disk"`, image sha256 unchanged) | +| 99 | NVMe normal shutdown before bus-master disable (CC.SHN = 01b, then wait CSTS.SHST = 10b) | NVME_SHUTDOWN | DIFFERS (C-only; the Rust kernel has no NVMe shutdown. Boots 1-4 and 6, both geometries: exactly one `nvme: shutdown normal cc=0x..->0x.. csts=0x.. shst=complete waited_us=N` line with CC.SHN read back as 01b and CSTS.SHST as 10b, printed before `dma_gate: nvme bus master revoked`, and no `nvme: shutdown fallback disable` line. Host tests cover CFS, not-ready, already-shut, timeout and the CC.EN = 0 fallback) | +| 100 | QEMU device trace shows the shutdown while bus master is on | NVME_SHUTDOWN | DIFFERS (C-only: QEMU `-trace` of `pci_nvme_mmio_shutdown_set` and `pci_cfg_write` on the NVMe command register; the shutdown must land between the guest's BME-set and BME-clear writes) | +| 101 | reset path quiesces DMA first; no shutdown without a bound controller | NVME_SHUTDOWN | DIFFERS (C-only: `devices: quiesce before reset nvme=already-released` on every boot (`ck_reset` hook, also covers panic/fault resets); boot 5 safe image: absent `nvme: shutdown normal`) | ## Summary counts -Rows 1-98: IDENTICAL 15, DIFFERS 22, NOT_RUN 61 (rows 92-95 have no CK gate at all; rows 97-98 are C-only). Rows 13, 24, 47-72, 76 and 96-98 were re-verified against scripts/qemu_ck_store_test.sh and scripts/lib_ck_m1_checks.sh at the commit that adds this line. +Rows 1-101: IDENTICAL 15, DIFFERS 25, NOT_RUN 61 (rows 92-95 have no CK gate at all; rows 96-101 are C-only). Rows 13, 24, 47-72, 76 and 96-101 were re-verified against scripts/qemu_ck_store_test.sh and scripts/lib_ck_m1_checks.sh at the commit that adds this line. `scripts/trust1_m5_qualify.sh --with-qemu` also runs three of these gates as qemu rows: `ck_m1_boot_qemu` (M1), `ck_store_kernel_qemu` (M4_STORE) and diff --git a/native/kernel/Makefile b/native/kernel/Makefile index d1497e19..5b3fc75b 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -159,12 +159,12 @@ endef $(ELF): $(CORE_OBJS) $(BOOT)/image.ld $(call link_image,$@,$(CORE_OBJS)) - @s=$$($(KNM) $@ | grep -E ' [TtDdBbWwVv] ck_stage_(devices|security|store)$$' || true); \ + @s=$$($(KNM) $@ | grep -E ' [TtDdBbWwVv] ck_stage_(devices|security|store|quiesce)$$' || true); \ if [ -n "$$s" ]; then echo "core image defines stage symbols: $$s"; rm -f $@; exit 1; fi $(FULL_ELF): $(CORE_OBJS) $(STAGE_BUILT) $(BOOT)/image.ld $(call link_image,$@,$(CORE_OBJS) $(STAGE_BUILT)) - @for s in ck_stage_devices ck_stage_security ck_stage_store; do \ + @for s in ck_stage_devices ck_stage_security ck_stage_store ck_stage_quiesce; do \ $(KNM) $@ | grep -qE " [TW] $$s$$" || { echo "full image lacks $$s"; rm -f $@; exit 1; }; done $(EFI): $(ELF) diff --git a/native/kernel/README.md b/native/kernel/README.md index 48c24920..c069f91d 100644 --- a/native/kernel/README.md +++ b/native/kernel/README.md @@ -46,6 +46,18 @@ Output lands in `/target/native-kernel` (ignored by git). rc=` or `stage : not linked`. 6. `report_kind: final`, then PSCI SYSTEM_RESET (WFI loop if refused). +Every reset (final, panic or fault) first calls the stage hook +`ck_stage_quiesce` once, if linked: the devices stage releases a still-live +NVMe controller (NVMe normal shutdown, CC.SHN = 01b then wait for CSTS.SHST += 10b, bounded 5 s; if it cannot (fatal status, not ready, timeout) it clears +CC.EN and waits up to 1 s for CSTS.RDY = 0, printing `nvme: shutdown +fallback disable ...`; then bus master off; then the SMMU stream back to abort) +and prints `devices: quiesce before reset nvme=none|released-now|already-released`. +The normal store path already released it, printing `nvme: shutdown normal +cc=..->.. csts=.. shst=complete waited_us=N` before `dma_gate: nvme bus master +revoked`. Code: `dev/nvme_shutdown.c` (register sequence, host-tested in +`svc/tests/stage_test.c`), `dev/devices.c`. + A panic prints `report_kind: panic`; an unexpected exception prints `report_kind: fault` with ESR, FAR and ELR. Both then reset. Before the drop to EL1, VBAR_EL2 points at a separate fatal-only table (`ck_vectors_el2`): diff --git a/native/kernel/core/report.c b/native/kernel/core/report.c index bb7aff4a..9e08a711 100644 --- a/native/kernel/core/report.c +++ b/native/kernel/core/report.c @@ -42,7 +42,12 @@ static void psci_call(uint64_t fn) void ck_reset(void) { + static int quiescing; __asm__ volatile("msr daifset, #0xf" ::: "memory"); + if (ck_stage_quiesce && !quiescing) { + quiescing = 1; /* a panic inside the hook resets without it */ + ck_stage_quiesce(); + } ck_mb(); psci_call(0x84000009u); /* PSCI SYSTEM_RESET */ psci_call(0x84000008u); /* SYSTEM_OFF if reset was refused */ diff --git a/native/kernel/dev/devices.c b/native/kernel/dev/devices.c index 1703b5ee..471fd643 100644 --- a/native/kernel/dev/devices.c +++ b/native/kernel/dev/devices.c @@ -15,10 +15,14 @@ void ck_dev_nvme_release(void) if (!g_nvme.pf) return; g_nvme.bound = 0; if (g_nvme.bm_on) { - g_nvme.bm_on = 0; - if (pci_bus_master_off(g_nvme.pf) == 0) + /* Halt order: NVMe normal shutdown (CC.SHN, wait CSTS.SHST) while the + * controller can still DMA, then bus master off, then the SMMU + * stream back to abort. */ + ck_nvme_shutdown_bound(&g_nvme); + if (pci_bus_master_off(g_nvme.pf) == 0) { + g_nvme.bm_on = 0; ck_printf("dma_gate: nvme bus master revoked\n"); - else + } else /* bm_on stays set: the reset quiesce hook retries the revoke */ ck_printf("dma_gate: nvme bus master revoke FAILED (command register still has BME)\n"); } if (g_nvme.confined) { @@ -44,3 +48,16 @@ int ck_stage_devices(void) if (nrc == -1) return 0; /* no NVMe present: not a devices failure; Store reports it */ return nrc; } + +/* Called by the core's ck_reset (PSCI reset/off after the final report, a + * panic or a fault report): no device may keep DMA across a reset. A normal + * boot has already released the NVMe controller in the Store stage, so this + * only reports that; after a panic mid-stage it does the full halt order. */ +void ck_stage_quiesce(void) +{ + int was_live = g_nvme.pf && (g_nvme.bm_on || g_nvme.confined); + if (was_live) + ck_dev_nvme_release(); + ck_printf("devices: quiesce before reset nvme=%s\n", + !g_nvme.pf ? "none" : was_live ? "released-now" : "already-released"); +} diff --git a/native/kernel/dev/nvme_bind.c b/native/kernel/dev/nvme_bind.c index 480c9386..1ecd00dd 100644 --- a/native/kernel/dev/nvme_bind.c +++ b/native/kernel/dev/nvme_bind.c @@ -1,5 +1,6 @@ /* nvme_bind.c -- NVMe binding through ck.h (see nvme_bind.h). Freestanding. */ #include "nvme_bind.h" +#include "nvme_shutdown.h" #include "ck.h" #include "disk_layout.h" @@ -132,6 +133,7 @@ int ck_nvme_bind(ck_nvme *n, const pci_system *pci) n->bound = 0; n->bm_on = 0; n->confined = 0; + n->shut = 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, TEST-ONLY)\n"); @@ -230,3 +232,24 @@ int ck_nvme_bind(ck_nvme *n, const pci_system *pci) n->bound = 1; return 0; } + +int ck_nvme_shutdown_bound(ck_nvme *n) +{ + if (!n || !n->bar0 || !n->bm_on || n->shut) + return CK_NVME_SHUT_EARG; + n->shut = 1; + struct ck_nvme_shut_ops o = {n, m_r32, m_w32, m_barrier, m_delay}; + struct ck_nvme_shut_result r = {0, 0, 0, 0}; + int rc = ck_nvme_shutdown(&o, CK_NVME_SHUT_TIMEOUT_US, &r); + ck_printf("nvme: shutdown normal cc=0x%08x->0x%08x csts=0x%08x shst=%s waited_us=%u\n", r.cc_before, + r.cc_after, r.csts, ck_nvme_shutdown_str(rc), r.waited_us); + if (ck_nvme_shutdown_needs_disable(rc)) { + /* The controller may still be live: stop it (CC.EN = 0, wait for + * CSTS.RDY = 0) before the caller cuts bus mastering. */ + struct ck_nvme_shut_result d = {0, 0, 0, 0}; + int drc = ck_nvme_disable(&o, CK_NVME_DISABLE_TIMEOUT_US, &d); + ck_printf("nvme: shutdown fallback disable cc=0x%08x->0x%08x csts=0x%08x rdy0=%s waited_us=%u\n", + d.cc_before, d.cc_after, d.csts, ck_nvme_shutdown_str(drc), d.waited_us); + } + return rc; +} diff --git a/native/kernel/dev/nvme_bind.h b/native/kernel/dev/nvme_bind.h index 1eb2b5c5..1f1b1ec8 100644 --- a/native/kernel/dev/nvme_bind.h +++ b/native/kernel/dev/nvme_bind.h @@ -17,6 +17,7 @@ typedef struct { int bound; int bm_on; /* bus mastering was enabled for this function */ int confined; /* DMA confined by the SMMU (ck_dma_confine) */ + int shut; /* normal shutdown already attempted (ck_nvme_shutdown_bound) */ uint32_t stream_id; /* SMMU stream when confined */ } ck_nvme; @@ -27,6 +28,12 @@ typedef struct { * no NVMe function exists. */ int ck_nvme_bind(ck_nvme *n, const pci_system *pci); +/* Normal NVMe shutdown (dev/nvme_shutdown.c) on the bound controller, run + * while bus mastering is still on, before the bus-master revoke. Prints + * "nvme: shutdown normal cc=0x..->0x.. csts=0x.. shst= waited_us=N". + * Runs at most once per bind; returns the CK_NVME_SHUT_* code. */ +int ck_nvme_shutdown_bound(ck_nvme *n); + /* Run the write/flush/read-back probe on the last whole unit of `d` * (shared with the host tests). `seed` varies the pattern per boot. * The unit's original bytes are read first and written back (flushed and diff --git a/native/kernel/dev/nvme_shutdown.c b/native/kernel/dev/nvme_shutdown.c new file mode 100644 index 00000000..7a8d04e7 --- /dev/null +++ b/native/kernel/dev/nvme_shutdown.c @@ -0,0 +1,87 @@ +/* nvme_shutdown.c -- see nvme_shutdown.h. NVMe Base 2.0 section 3.6.2 + * (normal shutdown): set CC.SHN to 01b, then wait for CSTS.SHST = 10b. + * SHN is only written to an enabled, ready, non-fatal controller that has not + * already shut down; otherwise the caller falls back to ck_nvme_disable. */ +#include "nvme_shutdown.h" + +int ck_nvme_shutdown(const struct ck_nvme_shut_ops *o, uint32_t timeout_us, struct ck_nvme_shut_result *r) +{ + if (!o || !o->r32 || !o->w32 || !o->delay || !r) + return CK_NVME_SHUT_EARG; + r->waited_us = 0; + r->cc_before = o->r32(o->ctx, CK_NVME_REG_CC); + r->cc_after = r->cc_before; + r->csts = o->r32(o->ctx, CK_NVME_REG_CSTS); + if (r->cc_before == 0xffffffffu || r->csts == 0xffffffffu) + return CK_NVME_SHUT_GONE; + if (r->csts & CK_NVME_CSTS_CFS) + return CK_NVME_SHUT_FATAL; + if (!(r->cc_before & CK_NVME_CC_EN)) + return CK_NVME_SHUT_NOT_ENABLED; + if ((r->csts & CK_NVME_CSTS_SHST_MASK) == CK_NVME_CSTS_SHST_DONE) + return CK_NVME_SHUT_ALREADY; + if (!(r->csts & CK_NVME_CSTS_RDY)) + return CK_NVME_SHUT_NOT_READY; + o->w32(o->ctx, CK_NVME_REG_CC, (r->cc_before & ~CK_NVME_CC_SHN_MASK) | CK_NVME_CC_SHN_NORMAL); + if (o->barrier) + o->barrier(o->ctx); + r->cc_after = o->r32(o->ctx, CK_NVME_REG_CC); + for (;;) { + r->csts = o->r32(o->ctx, CK_NVME_REG_CSTS); + if (r->csts == 0xffffffffu) + return CK_NVME_SHUT_GONE; + if ((r->csts & CK_NVME_CSTS_SHST_MASK) == CK_NVME_CSTS_SHST_DONE) + return CK_NVME_SHUT_OK; + if (r->csts & CK_NVME_CSTS_CFS) + return CK_NVME_SHUT_FATAL; + if (r->waited_us >= timeout_us) + return CK_NVME_SHUT_TIMEOUT; + o->delay(o->ctx, CK_NVME_SHUT_POLL_US); + r->waited_us += CK_NVME_SHUT_POLL_US; + } +} + +int ck_nvme_shutdown_needs_disable(int rc) +{ + return rc == CK_NVME_SHUT_TIMEOUT || rc == CK_NVME_SHUT_FATAL || rc == CK_NVME_SHUT_NOT_READY; +} + +int ck_nvme_disable(const struct ck_nvme_shut_ops *o, uint32_t timeout_us, struct ck_nvme_shut_result *r) +{ + if (!o || !o->r32 || !o->w32 || !o->delay || !r) + return CK_NVME_SHUT_EARG; + r->waited_us = 0; + r->cc_before = o->r32(o->ctx, CK_NVME_REG_CC); + r->cc_after = r->cc_before; + if (r->cc_before == 0xffffffffu) + return CK_NVME_SHUT_GONE; + o->w32(o->ctx, CK_NVME_REG_CC, r->cc_before & ~(CK_NVME_CC_EN | CK_NVME_CC_SHN_MASK)); + if (o->barrier) + o->barrier(o->ctx); + r->cc_after = o->r32(o->ctx, CK_NVME_REG_CC); + for (;;) { + r->csts = o->r32(o->ctx, CK_NVME_REG_CSTS); + if (r->csts == 0xffffffffu) + return CK_NVME_SHUT_GONE; + if (!(r->csts & CK_NVME_CSTS_RDY)) + return CK_NVME_SHUT_OK; + if (r->waited_us >= timeout_us) + return CK_NVME_SHUT_TIMEOUT; + o->delay(o->ctx, CK_NVME_SHUT_POLL_US); + r->waited_us += CK_NVME_SHUT_POLL_US; + } +} + +const char *ck_nvme_shutdown_str(int rc) +{ + switch (rc) { + case CK_NVME_SHUT_OK: return "complete"; + case CK_NVME_SHUT_ALREADY: return "already-complete"; + case CK_NVME_SHUT_NOT_ENABLED: return "not-enabled"; + case CK_NVME_SHUT_TIMEOUT: return "TIMEOUT"; + case CK_NVME_SHUT_GONE: return "DEVICE-GONE"; + case CK_NVME_SHUT_FATAL: return "CONTROLLER-FATAL"; + case CK_NVME_SHUT_NOT_READY: return "NOT-READY"; + default: return "EARG"; + } +} diff --git a/native/kernel/dev/nvme_shutdown.h b/native/kernel/dev/nvme_shutdown.h new file mode 100644 index 00000000..2d412bc7 --- /dev/null +++ b/native/kernel/dev/nvme_shutdown.h @@ -0,0 +1,62 @@ +/* nvme_shutdown.h -- NVMe normal shutdown (CC.SHN = 01b, wait for + * CSTS.SHST = 10b) for the C kernel's halt/reboot path. Register access goes + * through ops so the sequence is host-testable against a model controller + * (svc/tests/stage_test.c). native/disk is not touched: this sits in + * native/kernel/dev next to the bind code that owns the BAR. */ +#ifndef CK_NVME_SHUTDOWN_H +#define CK_NVME_SHUTDOWN_H +#include + +#define CK_NVME_REG_CC 0x14u +#define CK_NVME_REG_CSTS 0x1cu +#define CK_NVME_CC_EN 0x1u +#define CK_NVME_CC_SHN_MASK (3u << 14) +#define CK_NVME_CC_SHN_NORMAL (1u << 14) +#define CK_NVME_CSTS_RDY 0x1u +#define CK_NVME_CSTS_CFS (1u << 1) +#define CK_NVME_CSTS_SHST_MASK (3u << 2) +#define CK_NVME_CSTS_SHST_DONE (2u << 2) +#define CK_NVME_SHUT_POLL_US 100u +#define CK_NVME_SHUT_TIMEOUT_US 5000000u /* no RTD3E use yet: a fixed 5 s bound */ +#define CK_NVME_DISABLE_TIMEOUT_US 1000000u /* fallback CC.EN = 0, bounded 1 s */ + +enum { + CK_NVME_SHUT_OK = 0, /* SHST reached 10b */ + CK_NVME_SHUT_NOT_ENABLED = 1, /* CC.EN = 0: controller idle, nothing to shut down */ + CK_NVME_SHUT_TIMEOUT = -1, /* SHST never reached 10b within the bound */ + CK_NVME_SHUT_GONE = -2, /* CSTS reads all ones: device not answering */ + CK_NVME_SHUT_EARG = -3, + CK_NVME_SHUT_FATAL = -4, /* CSTS.CFS = 1: controller fatal, cannot shut down normally */ + CK_NVME_SHUT_NOT_READY = -5, /* CC.EN = 1 but CSTS.RDY = 0: SHN must not be written */ + CK_NVME_SHUT_ALREADY = 2, /* SHST already 10b: nothing written */ +}; + +struct ck_nvme_shut_ops { + void *ctx; + uint32_t (*r32)(void *ctx, uint32_t off); + void (*w32)(void *ctx, uint32_t off, uint32_t v); + void (*barrier)(void *ctx); + void (*delay)(void *ctx, uint32_t us); +}; + +struct ck_nvme_shut_result { + uint32_t cc_before; /* CC as found */ + uint32_t cc_after; /* CC read back after the SHN write */ + uint32_t csts; /* last CSTS read */ + uint32_t waited_us; /* polling time spent */ +}; + +/* Requests a normal shutdown and waits (bounded) for it to complete. Bus + * mastering must still be on: the controller may DMA while it finishes. */ +int ck_nvme_shutdown(const struct ck_nvme_shut_ops *o, uint32_t timeout_us, struct ck_nvme_shut_result *r); +const char *ck_nvme_shutdown_str(int rc); + +/* Fallback when a normal shutdown cannot complete (TIMEOUT, FATAL, + * NOT_READY): clear CC.EN (and SHN) and wait, bounded, for CSTS.RDY = 0 so + * the controller stops issuing DMA before bus mastering is cut. Returns OK, + * TIMEOUT, GONE or EARG; r->cc_after is CC read back after the write. */ +int ck_nvme_disable(const struct ck_nvme_shut_ops *o, uint32_t timeout_us, struct ck_nvme_shut_result *r); +/* 1 when rc means the controller may still be live and must be disabled. */ +int ck_nvme_shutdown_needs_disable(int rc); + +#endif diff --git a/native/kernel/include/ck.h b/native/kernel/include/ck.h index 7622217b..f27c342b 100644 --- a/native/kernel/include/ck.h +++ b/native/kernel/include/ck.h @@ -115,5 +115,8 @@ int ck_dma_faults(uint32_t stream_id, struct ck_dma_fault *first); __attribute__((weak)) int ck_stage_devices(void); /* PCI, NVMe, virtio-net */ __attribute__((weak)) int ck_stage_security(void); /* capability + ARGUS */ __attribute__((weak)) int ck_stage_store(void); /* sealed Store on NVMe */ +/* Optional devices-stage hook: ck_reset calls it once, before PSCI reset or + * off (final report, panic, fault), so no device keeps DMA across a reset. */ +__attribute__((weak)) void ck_stage_quiesce(void); #endif diff --git a/native/kernel/stage.mk b/native/kernel/stage.mk index 276d25f1..e06ea045 100644 --- a/native/kernel/stage.mk +++ b/native/kernel/stage.mk @@ -13,7 +13,7 @@ STAGE_NATIVE := $(STAGE_DIR)/.. STAGE_OUT ?= /tmp/aienos-ck-stage-$(shell id -u) # Own sources (image). -STAGE_OWN_SRCS := $(STAGE_DIR)/dev/pci.c $(STAGE_DIR)/dev/nvme_bind.c $(STAGE_DIR)/dev/virtio_net.c \ +STAGE_OWN_SRCS := $(STAGE_DIR)/dev/pci.c $(STAGE_DIR)/dev/nvme_bind.c $(STAGE_DIR)/dev/nvme_shutdown.c $(STAGE_DIR)/dev/virtio_net.c \ $(STAGE_DIR)/dev/devices.c $(STAGE_DIR)/svc/security.c $(STAGE_DIR)/svc/store_boot.c # Native modules, compiled unmodified. STAGE_NATIVE_SRCS := \ diff --git a/native/kernel/svc/tests/stage_test.c b/native/kernel/svc/tests/stage_test.c index 2b4d3fac..08aa6ad1 100644 --- a/native/kernel/svc/tests/stage_test.c +++ b/native/kernel/svc/tests/stage_test.c @@ -8,6 +8,7 @@ #include "ck.h" #include "ck_host.h" #include "devices.h" +#include "nvme_shutdown.h" #include "disk_file.h" #include "disk_layout.h" #include "m5.h" @@ -330,6 +331,107 @@ static void test_security(void) ck_security_shutdown(); } + +/* ---- NVMe normal shutdown (dev/nvme_shutdown.c) against a model controller */ +struct shut_model { + uint32_t cc, csts; + int done_after; /* polls until SHST = 10b; -1 never */ + int polls, cc_writes, gone; + int cfs_after; /* polls until CFS = 1; 0 never */ + int rdy_drop_after; /* polls after EN = 0 until RDY = 0; -1 never */ + int dpolls; + uint32_t log_off[8]; + int nlog; +}; +static uint32_t sm_r32(void *ctx, uint32_t off) +{ + struct shut_model *m = ctx; + if (m->gone) return 0xffffffffu; + if (off == CK_NVME_REG_CC) return m->cc; + if (off == CK_NVME_REG_CSTS) { + if (!(m->cc & CK_NVME_CC_EN) && m->rdy_drop_after >= 0 && m->dpolls++ >= m->rdy_drop_after) + m->csts &= ~CK_NVME_CSTS_RDY; + if (m->cc & CK_NVME_CC_SHN_MASK) { + int p = m->polls++; + if (m->done_after >= 0 && p >= m->done_after) + m->csts = (m->csts & ~CK_NVME_CSTS_SHST_MASK) | CK_NVME_CSTS_SHST_DONE; + if (m->cfs_after > 0 && p >= m->cfs_after) + m->csts |= CK_NVME_CSTS_CFS; + } + return m->csts; + } + return 0; +} +static void sm_w32(void *ctx, uint32_t off, uint32_t v) +{ + struct shut_model *m = ctx; + if (m->nlog < 8) m->log_off[m->nlog++] = off; + if (off == CK_NVME_REG_CC) { m->cc = v; m->cc_writes++; } +} +static void sm_delay(void *ctx, uint32_t us) { (void)ctx; (void)us; } + +static void test_nvme_shutdown(void) +{ + struct shut_model m; + struct ck_nvme_shut_ops o = {&m, sm_r32, sm_w32, 0, sm_delay}; + struct ck_nvme_shut_result r; + /* Normal: EN=1, IOSQES/IOCQES kept, SHN=01 written once, SHST reached. */ + memset(&m, 0, sizeof m); + m.cc = 0x00460001u; m.csts = 1; m.done_after = 3; + CHECK(ck_nvme_shutdown(&o, 1000, &r) == CK_NVME_SHUT_OK); + CHECK(m.cc == (0x00460001u | CK_NVME_CC_SHN_NORMAL) && m.cc_writes == 1 && m.log_off[0] == CK_NVME_REG_CC); + CHECK(r.cc_before == 0x00460001u && (r.cc_after & CK_NVME_CC_SHN_MASK) == CK_NVME_CC_SHN_NORMAL); + CHECK((r.csts & CK_NVME_CSTS_SHST_MASK) == CK_NVME_CSTS_SHST_DONE && r.waited_us == 3 * CK_NVME_SHUT_POLL_US); + /* An abrupt-shutdown request already in CC is replaced by normal. */ + memset(&m, 0, sizeof m); + m.cc = 0x00468001u; m.csts = 1; m.done_after = 1; /* not yet complete at entry */ + CHECK(ck_nvme_shutdown(&o, 1000, &r) == CK_NVME_SHUT_OK && (m.cc & CK_NVME_CC_SHN_MASK) == CK_NVME_CC_SHN_NORMAL); + /* Never completes: bounded, reports TIMEOUT. */ + memset(&m, 0, sizeof m); + m.cc = 1; m.csts = 1; m.done_after = -1; + CHECK(ck_nvme_shutdown(&o, 1000, &r) == CK_NVME_SHUT_TIMEOUT && r.waited_us == 1000 && m.cc_writes == 1); + /* Disabled controller: nothing written. */ + memset(&m, 0, sizeof m); + CHECK(ck_nvme_shutdown(&o, 1000, &r) == CK_NVME_SHUT_NOT_ENABLED && m.cc_writes == 0); + /* Device not answering. */ + memset(&m, 0, sizeof m); + m.gone = 1; + CHECK(ck_nvme_shutdown(&o, 1000, &r) == CK_NVME_SHUT_GONE && m.cc_writes == 0); + CHECK(ck_nvme_shutdown(0, 1000, &r) == CK_NVME_SHUT_EARG); + CHECK(ck_nvme_shutdown(&o, 1000, 0) == CK_NVME_SHUT_EARG); + CHECK(ck_nvme_shutdown_str(CK_NVME_SHUT_OK)[0] == 'c'); + /* Fatal controller (CFS at entry): nothing written, needs disable. */ + memset(&m, 0, sizeof m); + m.cc = 1; m.csts = 1 | CK_NVME_CSTS_CFS; + CHECK(ck_nvme_shutdown(&o, 1000, &r) == CK_NVME_SHUT_FATAL && m.cc_writes == 0 + && ck_nvme_shutdown_needs_disable(CK_NVME_SHUT_FATAL)); + /* CFS raised while polling: stop at once, not after the full bound. */ + memset(&m, 0, sizeof m); + m.cc = 1; m.csts = 1; m.done_after = -1; m.cfs_after = 2; + CHECK(ck_nvme_shutdown(&o, 1000000, &r) == CK_NVME_SHUT_FATAL && r.waited_us == 2 * CK_NVME_SHUT_POLL_US); + /* Enabled but not ready: SHN must not be written. */ + memset(&m, 0, sizeof m); + m.cc = 1; m.csts = 0; + CHECK(ck_nvme_shutdown(&o, 1000, &r) == CK_NVME_SHUT_NOT_READY && m.cc_writes == 0); + /* Already shut down: nothing written, no disable needed. */ + memset(&m, 0, sizeof m); + m.cc = 1 | CK_NVME_CC_SHN_NORMAL; m.csts = 1 | CK_NVME_CSTS_SHST_DONE; + CHECK(ck_nvme_shutdown(&o, 1000, &r) == CK_NVME_SHUT_ALREADY && m.cc_writes == 0 + && !ck_nvme_shutdown_needs_disable(CK_NVME_SHUT_ALREADY) && !ck_nvme_shutdown_needs_disable(CK_NVME_SHUT_OK)); + /* Disable fallback: EN and SHN cleared, other CC fields kept, RDY drops. */ + memset(&m, 0, sizeof m); + m.cc = 0x00464001u; m.csts = 1; m.rdy_drop_after = 2; + CHECK(ck_nvme_disable(&o, 1000, &r) == CK_NVME_SHUT_OK && m.cc == 0x00460000u && m.cc_writes == 1 + && !(r.csts & CK_NVME_CSTS_RDY) && r.waited_us == 2 * CK_NVME_SHUT_POLL_US); + /* Disable that never drops RDY is bounded; gone device is reported. */ + memset(&m, 0, sizeof m); + m.cc = 1; m.csts = 1; m.rdy_drop_after = -1; + CHECK(ck_nvme_disable(&o, 1000, &r) == CK_NVME_SHUT_TIMEOUT && r.waited_us == 1000); + memset(&m, 0, sizeof m); + m.gone = 1; + CHECK(ck_nvme_disable(&o, 1000, &r) == CK_NVME_SHUT_GONE && m.cc_writes == 0); + CHECK(ck_nvme_disable(0, 1000, &r) == CK_NVME_SHUT_EARG); +} int main(void) { setvbuf(stdout, NULL, _IOLBF, 0); @@ -339,6 +441,7 @@ int main(void) test_pci_enum(); test_store(); test_security(); + test_nvme_shutdown(); printf("CK_STAGE_HOST: %s checks=%d failures=%d\n", failures ? "FAIL" : "PASS", checks, failures); return failures ? 1 : 0; } diff --git a/scripts/ck_gates.sh b/scripts/ck_gates.sh index a2295ba7..ab5787e7 100755 --- a/scripts/ck_gates.sh +++ b/scripts/ck_gates.sh @@ -6,7 +6,7 @@ # (scripts/qemu_ck_boot_test.sh, scripts/qemu_ck_store_test.sh), and prints # one line per gate: # AIENOS_CK_: PASS|FAIL|NOT_RUN [(reason)] -# for M1 M3 SMMU P2_ARTIFACT M0_ROLLBACK M4_NVME M4_STORE M4_STORE_CRASH +# for M1 M3 SMMU NVME_SHUTDOWN P2_ARTIFACT M0_ROLLBACK M4_NVME M4_STORE M4_STORE_CRASH # M4_CONTINUITY M4_RECOVERY ARGUS1_REVOKE KEYBOARD. Gates with no C # implementation print NOT_RUN (MISSING_IMPLEMENTATION: ) and never # run anything. A missing child script, a child that reports NOT_RUN, a child @@ -43,6 +43,7 @@ CK_GATE_TABLE=' M1|boot|- M3|missing|no threads, EL0 tasks, preemption, MADT placement or typed IPC in the C kernel SMMU|store|- +NVME_SHUTDOWN|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|- @@ -330,23 +331,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_SMMU: PASS' 'AIENOS_CK_M3: PASS' 'AIENOS_CK_KEYBOARD: PASS' 'PASS NVMe DMA granted confined' \ + 'AIENOS_CK_SMMU: PASS' 'AIENOS_CK_NVME_SHUTDOWN: 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 + expect M1 PASS; expect M4_NVME PASS; expect M4_STORE PASS; expect ARGUS1_REVOKE PASS; expect SMMU PASS; expect NVME_SHUTDOWN PASS; expect_missing_all count_rows - [[ "${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}" + [[ "${n_pass}/${n_fail}/${n_notrun}/${n_total}" == "6/0/7/13" && ${overall} == NOT_ALL_GATES_PASS ]] \ + && ok "A: counts 6/0/7 of 13, 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 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" + [[ "$(grep -c '^AIENOS_CK_[A-Z0-9_]*: ' "${tmp}/A.out")" == 13 ]] && ok "A: exactly 13 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 SMMU 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 NVME_SHUTDOWN 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. @@ -382,7 +383,7 @@ self_test() { bad "receipt name does not match its content hash" fi if command -v jq >/dev/null; then - jq -e '.physical == "NOT_RUN" and (.gates | length) == 12 and .verdict == "NOT_ALL_GATES_PASS" + jq -e '.physical == "NOT_RUN" and (.gates | length) == 13 and .verdict == "NOT_ALL_GATES_PASS" and ([.gates[] | select(.id == "M1")][0].verdict == "PASS") and ([.gates[] | select(.id == "ARGUS1_REVOKE")][0].verdict == "FAIL") and (.children | length) == 2 and .commit_subject == "quote \" backslash \\ tab\tend"' "${receipt_path}" >/dev/null \ @@ -398,7 +399,7 @@ self_test() { bad "an existing receipt was overwritten or the clash was not refused" fi - # The gate table parses: 12 rows, known sources, reasons only on missing rows. + # The gate table parses: 13 rows, known sources, reasons only on missing rows. local g s w rows=0 tbad=0 while IFS='|' read -r g s w; do [[ -n "${g}" ]] || continue @@ -409,7 +410,7 @@ self_test() { *) tbad=1 ;; esac done <<<"${CK_GATE_TABLE}" - [[ ${tbad} == 0 && ${rows} == 12 ]] && ok "gate table: 12 rows well-formed" || bad "gate table malformed (${rows} rows)" + [[ ${tbad} == 0 && ${rows} == 13 ]] && ok "gate table: 13 rows well-formed" || bad "gate table malformed (${rows} rows)" # Dirty-tree refusal, end to end, on a private clone (never this tree). # Both runs stop before any child script (no QEMU). diff --git a/scripts/qemu_ck_store_test.sh b/scripts/qemu_ck_store_test.sh index 27bb0cfc..f675e71a 100755 --- a/scripts/qemu_ck_store_test.sh +++ b/scripts/qemu_ck_store_test.sh @@ -59,13 +59,20 @@ # 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. # Boots 1-4 add iommu=smmuv3 to the machine like the Rust Store scripts. +# NVMe shutdown (C-only gate, no Rust counterpart): on every boot where the +# NVMe bus master was on (boots 1-4 and 6) the kernel must print exactly one +# "nvme: shutdown normal cc=..->.. csts=.. shst=complete" line with CC.SHN read +# back as 01b and CSTS.SHST as 10b, before "dma_gate: nvme bus master +# revoked"; QEMU's own trace (pci_nvme_mmio_shutdown_set, pci_cfg_write) must +# show the shutdown landing between the BME set and BME clear writes. Boot 5 +# must show no shutdown. Every boot must show the ck_reset quiesce hook. # # 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, -# AIENOS_CK_SMMU, each PASS|FAIL|NOT_RUN. A boot that fails an M1 check fails -# all four. +# AIENOS_CK_SMMU, AIENOS_CK_NVME_SHUTDOWN, each PASS|FAIL|NOT_RUN. A boot that +# fails an M1 check fails all five. # Needs qemu-system-aarch64 and AAVMF (Ubuntu: qemu-system-arm qemu-efi-aarch64). set -euo pipefail @@ -77,6 +84,7 @@ verdicts() { # one value for all three echo "AIENOS_CK_M4_STORE: $1" echo "AIENOS_CK_ARGUS1_REVOKE: $1" echo "AIENOS_CK_SMMU: $1" + echo "AIENOS_CK_NVME_SHUTDOWN: $1" } code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" @@ -108,6 +116,7 @@ release_flag() { fi } top="$(mktemp -d)" +printf '%s\n' pci_nvme_mmio_shutdown_set pci_nvme_mmio_shutdown_cleared pci_cfg_write >"${top}/trace.events" cleanup() { rm -rf "${top}" release_flag @@ -117,7 +126,7 @@ 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; smmu_fail=0 +m1_fail=0; nvme_fail=0; store_fail=0; argus_fail=0; smmu_fail=0; shut_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 @@ -148,7 +157,8 @@ boot() { -drive if=none,id=nvme0,format=raw,file="$3" \ -device "${nvme_dev}" \ -device ramfb -display none -nic none \ - -serial file:"${work}/serial.log" -no-reboot + -serial file:"${work}/serial.log" -no-reboot \ + -D "${work}/trace.log" -trace events="${top}/trace.events" qemu_status=$? set -e tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt" @@ -196,6 +206,45 @@ nvme_checks() { # block bytes, confined|bypass: the bound NVMe path check_absent "no bus master revoke failure (differs)" "bus master revoke FAILED" fail_into nvme_fail } +# shutdown_checks: the bound NVMe path must do an NVMe normal shutdown +# (CC.SHN = 01b, then CSTS.SHST = 10b) before the bus-master revoke. Three +# independent views: the kernel's register readback on serial, the serial +# order, and QEMU's own device trace (pci_nvme_mmio_shutdown_set must fall +# between the guest setting BME in the NVMe command register and clearing it). +shutdown_checks() { + local sre='^nvme: shutdown normal cc=0x([0-9a-f]{8})->0x([0-9a-f]{8}) csts=0x([0-9a-f]{8}) shst=([A-Za-z-]+) waited_us=([0-9]+)$' + local n line ls lr order + n=$(grep -cE "${sre}" "${work}/serial.txt" || true) + line=$(grep -E "${sre}" "${work}/serial.txt" | tail -1 || true) + if [[ "${n}" == 1 && "${line}" =~ ${sre} ]]; then + local cc_a=$(( 16#${BASH_REMATCH[2]} )) csts=$(( 16#${BASH_REMATCH[3]} )) res="${BASH_REMATCH[4]}" + if (( ((cc_a >> 14) & 3) == 1 )); then echo "PASS CC.SHN read back as 01b (normal shutdown requested)" + else echo "FAIL CC.SHN read back as $(( (cc_a >> 14) & 3 )), expected 1"; failed=1; fi + if (( ((csts >> 2) & 3) == 2 )) && [[ "${res}" == complete ]]; then + echo "PASS CSTS.SHST read back as 10b (shutdown complete, waited ${BASH_REMATCH[5]} us)" + else echo "FAIL shutdown not complete: ${line}"; failed=1; fi + else + echo "FAIL exactly one NVMe shutdown line reported (found ${n})"; failed=1 + fi + ls=$(grep -nE '^nvme: shutdown normal ' "${work}/serial.txt" | head -1 | cut -d: -f1) + lr=$(grep -n 'dma_gate: nvme bus master revoked' "${work}/serial.txt" | head -1 | cut -d: -f1) + if [[ -n "${ls}" && -n "${lr}" && "${ls}" -lt "${lr}" ]]; then echo "PASS shutdown reported before the bus-master revoke (serial order)" + else echo "FAIL shutdown not before the bus-master revoke (serial lines ${ls:-none} / ${lr:-none})"; failed=1; fi + order=$(grep -E '^pci_nvme_mmio_shutdown_set|^pci_cfg_write nvme [0-9a-f:.]+ @0x4 <- 0x[0-9a-f]+$' "${work}/trace.log" 2>/dev/null | { + on=0; s=0; out=none + while read -r a _ _ _ _ v; do + if [[ "${a}" == pci_nvme_mmio_shutdown_set ]]; then (( on )) && s=1 + elif (( v & 4 )); then on=1; s=0 + elif (( on )); then if (( s )); then out=ok; else out=bad; fi; on=0 + fi + done + echo "${out}"; }) + [[ "${order}" == ok ]] && echo "PASS QEMU device trace: shutdown set while BME on, then BME cleared" \ + || { echo "FAIL QEMU device trace order (${order})"; failed=1; } + check_absent "no fallback disable needed (normal shutdown completed)" "nvme: shutdown fallback disable" + check "reset quiesce hook ran after the release (differs)" "devices: quiesce before reset nvme=already-released" + fail_into shut_fail +} argus_checks() { check "capabilities: grant, attenuate, deny amplify/forged/revoked, RNDR token (differs)" \ "caps: ok granted=yes attenuated=yes amplify=denied forged=denied revoked=denied office_token=rndr$" @@ -214,6 +263,7 @@ for bs in 512 4096; do for k in 1 2 3; do boot "${bs}-boot${k}" "${safe_efi}" "${image}" "${bs}" "${serial}" smmu nvme_checks "${bs}" confined + shutdown_checks argus_checks check "stage store ok (differs)" "stage store: ok" check_absent "Store not refused (differs)" "store: REFUSED" @@ -276,6 +326,7 @@ for bs in 512 4096; do || store_fail_msg "host corruption did not change the image" boot "${bs}-boot4-corrupt" "${safe_efi}" "${image}" "${bs}" "${serial}" smmu nvme_checks "${bs}" confined + shutdown_checks 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" @@ -295,6 +346,8 @@ for bs in 512 4096; do check_absent "NVMe controller never granted DMA" "dma_gate: nvme granted" check_absent "no unsafe DMA bypass in this image" "UNSAFE NVME DMA BYPASS" check_absent "NVMe controller never identified (differs)" "nvme: identify" + check_absent "no NVMe shutdown without a bound controller (differs)" "nvme: shutdown normal" + check "reset quiesce hook found nothing live (differs)" "devices: quiesce before reset nvme=already-released" check "NVMe left unbound (differs)" "devices: pci=ok nvme=unbound" fail_into nvme_fail check "Store refused without a disk (differs)" 'store: REFUSED proof=io step="no boot disk"' @@ -308,6 +361,7 @@ for bs in 512 4096; do # 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 + shutdown_checks 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" @@ -318,10 +372,10 @@ for bs in 512 4096; do done release_flag -if [[ "${m1_fail}${nvme_fail}${store_fail}${argus_fail}${smmu_fail}" != 00000 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then +if [[ "${m1_fail}${nvme_fail}${store_fail}${argus_fail}${smmu_fail}${shut_fail}" != 000000 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then for s in "${top}"/*/serial.txt; do echo "---- serial console $(basename "$(dirname "${s}")") (stage lines) ----" - grep -E '^(stage |smmu|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:|pci_nvme_mmio_shutdown)' "${s}" | head -60 || true done fi [[ "${m1_fail}" != 0 ]] && echo "M1 checks failed on at least one boot: no verdict can pass" @@ -331,4 +385,5 @@ 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}")" echo "AIENOS_CK_SMMU: $(v "${smmu_fail}")" -[[ "${m1_fail}${nvme_fail}${store_fail}${argus_fail}${smmu_fail}" == 00000 ]] || exit 1 +echo "AIENOS_CK_NVME_SHUTDOWN: $(v "${shut_fail}")" +[[ "${m1_fail}${nvme_fail}${store_fail}${argus_fail}${smmu_fail}${shut_fail}" == 000000 ]] || exit 1