diff --git a/.crumb b/.crumb index 79bbf1d5..0a04b94f 100644 --- a/.crumb +++ b/.crumb @@ -33,8 +33,8 @@ "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "8d74f1310d995e22561d226cd8d1b9e682ff4d2087744288b9ffd2e1a5971598", - "last_commit": "0876fc1", + "digest": "f25d5173b3b773a434fe623f51db3f3ab79b41fa6bf46b66e2f1dd2ddf525594", + "last_commit": "e811eda", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, @@ -45,15 +45,15 @@ "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:602c7412daa910a2a9330a9330815e04b41293dfb1e8b24f656ba9641cb073d2", - "compiled_at": "2026-10-09T03:38:41Z", + "children_root": "sha256:b04feb3c6bde0bc83be3945cfe7fca50b15f2bf06ee5edb08d6d6b31a189481f", + "compiled_at": "2026-10-09T12:07:39Z", "compiler": "crumb-compile/1", - "digest": "sha256:0e1d957287f32f9f52be8526d92cfe00eb3df737118c2c0e23d6ee814a7e65e7", + "digest": "sha256:7a11342d4432f6e9447655f4ad3bb82beff4cb87f939f2bebc4a4057dc3499bf", "evidence_files": 155, "evidence_root": "sha256:d59c0cef4e7dd5172923754a95b411ca54b01f35baca4587f792f1b259f1eaf7", "files": 3028, - "generated_at_commit": "7f0de7e9753209b82f6a6d7b090afb1f8bfa20c8", - "source_tree": "sha256:12c29a520543c85fa1ad21d5b13af0cc57bfe142b3d9757cdd8a28a1d53185bd" + "generated_at_commit": "e811eda1e29c31f9c0364d29e5ae0a4f5e1c9349", + "source_tree": "sha256:a0bb3cd6cb5f552f73230ca26d596142e59ba6edc4faf0bbdf6a94216d38f97b" }, "owner_lane": "", "provenance": { diff --git a/native/.crumb b/native/.crumb index fd99d776..7f78b940 100644 --- a/native/.crumb +++ b/native/.crumb @@ -79,8 +79,8 @@ "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "b47571fa3b5eb47059f00780535c46cf82c21af7956bc4eb3eb7d75e7450acab", - "last_commit": "2bc7146", + "digest": "06229f7e9e08d1b65b41f18c7e615f64e4689bf6853d1d73b19a1829e4c97587", + "last_commit": "ee2ff16", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, @@ -109,15 +109,15 @@ "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:e18fc3db4c0756270c97037df877ec94f8bd21f7d67256c49c01aa636935d844", - "compiled_at": "2026-10-08T20:55:41Z", + "children_root": "sha256:843015b58d4bff5705662a9ef3b7520e1a131742d26f34fcab5d0c0a385dd76f", + "compiled_at": "2026-10-09T02:37:36Z", "compiler": "crumb-compile/1", - "digest": "sha256:8d74f1310d995e22561d226cd8d1b9e682ff4d2087744288b9ffd2e1a5971598", + "digest": "sha256:f25d5173b3b773a434fe623f51db3f3ab79b41fa6bf46b66e2f1dd2ddf525594", "evidence_files": 4, "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335", "files": 453, - "generated_at_commit": "2bc7146ee21806d0537c16fe70acabf5f0f6126f", - "source_tree": "sha256:70628cdac734ea7fca82204eaad18bc8d46cdae3facf7d304e17db58423a02fa" + "generated_at_commit": "23b2249dd1bbac3314e17b5d70357e71e226cf03", + "source_tree": "sha256:7b2ab90154cb69b4dc837965f01c31e85360b64fef31d8ddcea76d30487d59a1" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/.crumb b/native/kernel/.crumb index 42e717c1..38a52916 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -63,8 +63,8 @@ "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, { - "digest": "8bec4ed4dbf4745c8f311bebfbd58eab460696ec9651b1d3ac7b2f712e72a4cf", - "last_commit": "2bc7146", + "digest": "2dce85eb060adebc9b668fa932ad213985f63bf08b3738d6c8e02fe60db32a0f", + "last_commit": "ee2ff16", "name": "dev", "purpose": "Directory native/kernel/dev. Purpose not yet described by a human." }, @@ -81,8 +81,8 @@ "purpose": "Directory native/kernel/mm. Purpose not yet described by a human." }, { - "digest": "92746c0561f8e54e2164b2616421fe55acb34bd38aff2461110a437d483b4053", - "last_commit": "2bc7146", + "digest": "2712bd906a89792008981a3dc88d1f28f5c4ebcf278751ab852bfd0e76a62e1c", + "last_commit": "ee2ff16", "name": "svc", "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, @@ -99,15 +99,15 @@ "purpose": "Directory native/kernel/tools. Purpose not yet described by a human." } ], - "children_root": "sha256:0a2830745e83800fe517e2f9e374b269aeeb689bbda5368d02735bf3566190cf", - "compiled_at": "2026-10-08T20:55:41Z", + "children_root": "sha256:7b7fbe34bdb423c68314ee267f788571ba601474bb69a90d8d198a85cd9e8793", + "compiled_at": "2026-10-09T02:37:36Z", "compiler": "crumb-compile/1", - "digest": "sha256:b47571fa3b5eb47059f00780535c46cf82c21af7956bc4eb3eb7d75e7450acab", + "digest": "sha256:06229f7e9e08d1b65b41f18c7e615f64e4689bf6853d1d73b19a1829e4c97587", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 289, - "generated_at_commit": "2bc7146ee21806d0537c16fe70acabf5f0f6126f", - "source_tree": "sha256:086e9508300b8e643c6ecb3c8f1a18af237068cdafbdc979682d82700eded2d1" + "generated_at_commit": "23b2249dd1bbac3314e17b5d70357e71e226cf03", + "source_tree": "sha256:cfceb670b94cfe8d7e7cae23710621c2dc6e7041c7ca8a3155e1533109efd589" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/dev/.crumb b/native/kernel/dev/.crumb index 6f904bf9..3cabeead 100644 --- a/native/kernel/dev/.crumb +++ b/native/kernel/dev/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-08T20:55:41Z", + "compiled_at": "2026-10-09T02:37:36Z", "compiler": "crumb-compile/1", - "digest": "sha256:8bec4ed4dbf4745c8f311bebfbd58eab460696ec9651b1d3ac7b2f712e72a4cf", + "digest": "sha256:2dce85eb060adebc9b668fa932ad213985f63bf08b3738d6c8e02fe60db32a0f", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 27, - "generated_at_commit": "2bc7146ee21806d0537c16fe70acabf5f0f6126f", - "source_tree": "sha256:14784b04b7698f9d607bb95d9a7ac04aa1d615bf75025fa570314e8bdfadf5df" + "generated_at_commit": "23b2249dd1bbac3314e17b5d70357e71e226cf03", + "source_tree": "sha256:674f24cfa5d88e0b4f1c8834b6cdc9e991185b37242b0d2e75878cdbfeb6bcd6" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/dev/devices.c b/native/kernel/dev/devices.c index 16d44b9e..1a6ae3c8 100644 --- a/native/kernel/dev/devices.c +++ b/native/kernel/dev/devices.c @@ -19,7 +19,10 @@ const disk_dev *ck_dev_boot_disk(void) { return g_nvme.bound && g_nvme.part.vali void ck_dev_nvme_release(void) { - if (!g_nvme.pf) return; + if (!g_nvme.pf) { /* bind stopped before any device access: nothing to quiesce */ + g_nvme.claimed = 0; + return; + } g_nvme.bound = 0; if (g_nvme.bm_on) { /* Halt order: NVMe normal shutdown (CC.SHN, wait CSTS.SHST) while the @@ -40,6 +43,8 @@ void ck_dev_nvme_release(void) ck_printf("smmu: nvme stream 0x%x %s (rc=%d)\n", g_nvme.stream_id, urc == 0 ? "returned to abort" : "abort FAILED", urc); } + /* Ownership is given back only once DMA is fully off (bus master and stream). */ + if (!g_nvme.bm_on && !g_nvme.confined && !g_nvme.stuck) g_nvme.claimed = 0; } int ck_stage_devices(void) @@ -60,15 +65,14 @@ int ck_stage_devices(void) pci_sweep_report(&g_pci.ecam, &sw); } ck_xhci_after_sweep(&g_pci); /* TEST-ONLY bm-left-on mutation hook; no-op otherwise */ - int nrc = ck_nvme_bind(&g_nvme, &g_pci); - if (nrc) ck_dev_nvme_release(); /* a failed bind never keeps DMA */ - /* Report-only (aienos#31, cut S1): the NVMe the multi-segment discovery sees, and whether it is the - * function the segment-0 probe bound above. Binding is unchanged. */ - { - uint32_t dn = 0; - const pci_found *df = pci_stage_disc_found(&dn); - (void)pci_nvme_disc_report(df, dn, g_nvme.pf); - } + /* NVMe is bound from the multi-segment discovery result (aienos#31), any segment; the segment-0 + * enumeration above is only used for the other devices and for log wording. */ + uint32_t dn = 0; + const pci_found *df = pci_stage_disc_found(&dn); + int was_owned = g_nvme.claimed; /* a refusal as "already owned" must not tear the owner down */ + int nrc = ck_nvme_bind(&g_nvme, df, dn, &g_pci); + if (nrc && !was_owned) ck_dev_nvme_release(); /* a failed bind never keeps DMA */ + (void)pci_nvme_disc_report(df, dn, g_nvme.claimed ? g_nvme.pf : 0); virtio_pci_caps caps; const pci_func *vf = 0; int vrc = ck_virtio_net_probe(&g_pci, &caps, &vf); diff --git a/native/kernel/dev/nvme_bind.c b/native/kernel/dev/nvme_bind.c index 9c2366c7..6ca8ac18 100644 --- a/native/kernel/dev/nvme_bind.c +++ b/native/kernel/dev/nvme_bind.c @@ -128,13 +128,21 @@ int ck_disk_rw_probe(const disk_dev *d, uint32_t seed, uint64_t *lba_out) return match ? DISK_OK : DISK_EIO; } -int ck_nvme_bind(ck_nvme *n, const pci_system *pci) +int ck_nvme_bind(ck_nvme *n, const pci_found *found, uint32_t nfound, const pci_system *probe) { + /* Ownership first: one owner at a time. A refused call changes nothing. */ + if (n->claimed) { + ck_printf("nvme: bind refused, already owned by seg %04x %02x:%02x.%u (bound=%d bm_on=%d confined=%d)\n", + n->fn.segment, n->fn.bus, n->fn.dev, n->fn.fn, n->bound, n->bm_on, n->confined); + return NVME_ESTATE; + } n->bound = 0; n->bm_on = 0; n->confined = 0; n->shut = 0; + n->stuck = 0; n->stream_id = 0; + n->pf = 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"); #endif @@ -142,12 +150,44 @@ int ck_nvme_bind(ck_nvme *n, const pci_system *pci) ck_printf("WARNING: TEST-ONLY DISK TRANSLATION BYPASS BUILD (CK_TEST_DISK_XLATE_BYPASS=1, DISK_LAYOUT " "mutation; never counts toward a PASS)\n"); #endif - const pci_func *f = pci_find_class(pci, 0x010802u, 0xffffffu); - if (!f) { + uint32_t cands = 0; + const pci_found *cand = pci_disc_find_class(found, nfound, 0x010802u, 0xffffffu, &cands); + if (!cand) { ck_printf("nvme: no class 0x010802 function\n"); return -1; } - n->pf = f; + pci_bus_access acc; + if (pci_stage_disc_access(cand->segment, cand->bus, &acc)) { + ck_printf("nvme: unavailable (no mapped ECAM for seg %04x bus %02x)\n", cand->segment, cand->bus); + return NVME_EARG; + } + n->claimed = 1; /* recorded once, before the device is touched */ + if (pci_func_from_found(&n->fn, cand, &acc)) { + /* Refused before any write to the device: as if the bind was never attempted. */ + n->claimed = 0; + ck_printf("nvme: unavailable (seg %04x %02x:%02x.%u cannot be read as an endpoint; nothing written, not owned)\n", + cand->segment, cand->bus, cand->dev, cand->fn); + return NVME_EARG; + } + if (probe) /* keep the log wording for a function the segment-0 stage assigned */ + for (uint32_t i = 0; i < probe->n; i++) { + const pci_func *p = &probe->f[i]; + if (p->segment == n->fn.segment && p->bus == n->fn.bus && p->dev == n->fn.dev && p->fn == n->fn.fn) + for (uint32_t b = 0; b < PCI_MAX_BARS; b++) n->fn.bar[b].assigned_here = p->bar[b].assigned_here; + } + const pci_func *f = n->pf = &n->fn; + if (cands > 1) + ck_printf("nvme: %u class 0x010802 functions in discovery, binding the lowest segment/bus/dev/fn only\n", cands); + /* A function firmware left with bus mastering on must not be live before the DMA gate. */ + if (cand->command & 0x4u) { + int brc = pci_bus_master_off(f); + ck_printf("nvme: firmware left bus master on, cleared before the DMA gate (%s)\n", brc ? "STUCK" : "ok"); + if (brc) { + n->stuck = 1; /* release keeps the owner; only a reset ends it */ + ck_printf("nvme: unavailable (bus master would not clear; the device was written, so it stays owned, fail closed)\n"); + return NVME_ESTATE; + } + } const pci_bar *b = &f->bar[0]; ck_printf("nvme: discovery %02x:%02x.%u vid=0x%04x did=0x%04x bar0=0x%llx size=0x%llx%s\n", f->bus, f->dev, f->fn, f->vendor, f->device, (unsigned long long)b->addr, (unsigned long long)b->size, diff --git a/native/kernel/dev/nvme_bind.h b/native/kernel/dev/nvme_bind.h index e20c54b8..16ecf87d 100644 --- a/native/kernel/dev/nvme_bind.h +++ b/native/kernel/dev/nvme_bind.h @@ -15,6 +15,9 @@ typedef struct { disk_dev disk; volatile uint8_t *bar0; const pci_func *pf; + pci_func fn; /* the bound function, built from the discovery result (pf points here) */ + int claimed; /* ownership recorded once: a second bind is refused until the first is fully released */ + int stuck; /* device was written and bus master would not clear: stays owned through release (fail closed) */ int bound; int bm_on; /* bus mastering was enabled for this function */ int confined; /* DMA confined by the SMMU (ck_dma_confine) */ @@ -23,7 +26,17 @@ typedef struct { ck_part part; /* the AIENOS partition view (dev/disk_part.h): the only writable disk */ } ck_nvme; -/* Find the first class 01/08/02 function, gate DMA, map BAR0, init the +/* Bind the NVMe the multi-segment read-only discovery found (aienos#31): the + * first class 01/08/02 function in `found` (lowest segment, bus, device, + * function; any segment). Ownership is recorded once (n->claimed) before any + * device access: a second call while owned, or while DMA is not fully released, + * returns NVME_ESTATE and touches nothing. `probe` (may be NULL) is only used + * to keep the "assigned by stage" log wording. Returns -1 when discovery holds + * no NVMe. Bus mastering a function firmware left enabled is cleared before the + * DMA gate (refused if it sticks). DMA gate and everything after it are + * unchanged: + * + * gate DMA, map BAR0, init the * controller, fill n->disk, print identify/geometry/atomicity lines, run the * read-only checks (bounds refusal, read LBA 0), parse the GPT read-only and * select the AIENOS partition (n->part; none or an invalid GPT: print @@ -32,7 +45,7 @@ typedef struct { * partition end, then run the write+flush+read-back probe on the last 4 KiB * unit OF THE PARTITION. Returns 0, or a negative NVME_E* / DISK_E* / * CK_GPT_* / -1 when no NVMe function exists. */ -int ck_nvme_bind(ck_nvme *n, const pci_system *pci); +int ck_nvme_bind(ck_nvme *n, const pci_found *found, uint32_t nfound, const pci_system *probe); /* Normal NVMe shutdown (dev/nvme_shutdown.c) on the bound controller, run * while bus mastering is still on, before the bus-master revoke. Prints diff --git a/native/kernel/dev/pci.c b/native/kernel/dev/pci.c index ade648f8..3eba1f85 100644 --- a/native/kernel/dev/pci.c +++ b/native/kernel/dev/pci.c @@ -83,6 +83,12 @@ uint64_t pci_window_alloc(pci_window *w, uint64_t size) return a; } +#ifdef CK_HOST_TEST +/* Host tests only (never in the kernel image): a fake ECAM is plain memory and cannot answer the all-ones + * BAR sizing write with a size mask. When set, a non-zero return is the size to pretend BAR `i` has. */ +uint64_t (*pci_host_bar_size)(volatile uint8_t *cfg, uint32_t i); +#endif + static void size_bars(pci_func *f) { volatile uint8_t *c = f->cfg; @@ -108,6 +114,16 @@ static void size_bars(pci_func *f) if (is64) pci_w32(c, off + 4, 0xffffffffu); uint32_t lo = pci_r32(c, off); uint32_t hi = is64 ? pci_r32(c, off + 4) : 0; +#ifdef CK_HOST_TEST + if (pci_host_bar_size) { + uint64_t emu = pci_host_bar_size(c, i); + if (emu) { + uint64_t mask = ~(emu - 1u); + lo = (uint32_t)mask | (orig & 0xfu); + hi = is64 ? (uint32_t)(mask >> 32) : 0; + } + } +#endif pci_w32(c, off, orig); if (is64) pci_w32(c, off + 4, orig_hi); b->is64 = (uint8_t)is64; @@ -121,12 +137,10 @@ static void size_bars(pci_func *f) pci_w16(c, CFG_COMMAND, cmd); } -static int add_func(pci_system *s, uint8_t bus, uint8_t dev, uint8_t fn, volatile uint8_t *c) +static void fill_func(pci_func *f, uint16_t segment, uint8_t bus, uint8_t dev, uint8_t fn, volatile uint8_t *c) { - if (s->n >= PCI_MAX_FUNCS) return PCI_E_FULL; - pci_func *f = &s->f[s->n++]; uint32_t id = pci_r32(c, CFG_VENDOR); - f->segment = s->ecam.segment; + f->segment = segment; f->bus = bus; f->dev = dev; f->fn = fn; @@ -143,6 +157,12 @@ static int add_func(pci_system *s, uint8_t bus, uint8_t dev, uint8_t fn, volatil f->bar[i].is64 = f->bar[i].prefetch = f->bar[i].io = f->bar[i].assigned_here = 0; } size_bars(f); +} + +static int add_func(pci_system *s, uint8_t bus, uint8_t dev, uint8_t fn, volatile uint8_t *c) +{ + if (s->n >= PCI_MAX_FUNCS) return PCI_E_FULL; + fill_func(&s->f[s->n++], s->ecam.segment, bus, dev, fn, c); return PCI_OK; } @@ -217,6 +237,22 @@ int pci_enumerate(pci_system *s, pci_window *win) return PCI_OK; } +int pci_func_from_found(pci_func *out, const pci_found *f, const pci_bus_access *a) +{ + if (!out || !f || !a || !a->ecam) return PCI_E_ARG; + if ((f->header_type & 0x7fu) != 0) return PCI_E_ARG; /* endpoints only */ + volatile uint8_t *c = pci_cfg(a, f->bus, f->dev, f->fn); + if (!c) return PCI_E_ARG; + uint32_t id = pci_r32(c, CFG_VENDOR); + /* The ECAM must still answer with the identity discovery recorded. */ + if ((uint16_t)id != f->vendor || (uint16_t)(id >> 16) != f->device) return PCI_E_ARG; + fill_func(out, f->segment, f->bus, f->dev, f->fn, c); + out->bars_ok = 1; + for (uint32_t i = 0; i < PCI_MAX_BARS; i++) + if (!out->bar[i].io && out->bar[i].size && !out->bar[i].addr) out->bars_ok = 0; + return PCI_OK; +} + void pci_enable(const pci_func *f, int bm) { uint16_t cmd = pci_r16(f->cfg, CFG_COMMAND); @@ -225,8 +261,14 @@ void pci_enable(const pci_func *f, int bm) pci_w16(f->cfg, CFG_COMMAND, cmd); } +#ifdef CK_HOST_TEST +int pci_host_bme_stuck; /* host tests only: model a command register whose bus master bit will not clear */ +#endif int pci_bus_master_off(const pci_func *f) { +#ifdef CK_HOST_TEST + if (pci_host_bme_stuck) return -1; +#endif uint16_t cmd = pci_r16(f->cfg, CFG_COMMAND); pci_w16(f->cfg, CFG_COMMAND, (uint16_t)(cmd & ~CMD_BM)); return (pci_r16(f->cfg, CFG_COMMAND) & CMD_BM) ? -1 : 0; @@ -503,6 +545,22 @@ static pci_found g_disc_found[PCI_MAX_FUNCS]; static pci_found g_disc_all[PCI_MAX_FUNCS]; static uint32_t g_disc_all_n; +/* ECAM accessors the last report mapped, one per MCFG allocation. */ +static pci_bus_access g_disc_acc[PCI_MAX_SEGMENTS]; +static uint16_t g_disc_acc_seg[PCI_MAX_SEGMENTS]; +static uint32_t g_disc_acc_n; + +int pci_stage_disc_access(uint16_t segment, uint8_t bus, pci_bus_access *out) +{ + if (!out) return PCI_E_ARG; + for (uint32_t i = 0; i < g_disc_acc_n; i++) + if (g_disc_acc_seg[i] == segment && bus >= g_disc_acc[i].start_bus && bus <= g_disc_acc[i].end_bus) { + *out = g_disc_acc[i]; + return PCI_OK; + } + return PCI_E_ARG; +} + /* Report-only: prints what firmware declared and what answers, changes no * state. Each segment maps only its declared bus range through the * fail-closed ck_mmio_try_map (a refused map is reported, never retried @@ -527,6 +585,7 @@ int pci_stage_discover_report(void) } uint32_t scanned = 0, total = 0; g_disc_all_n = 0; + g_disc_acc_n = 0; for (uint32_t i = 0; i < nseg; i++) { const pci_ecam *e = &g_disc_ecam[i]; uint64_t first = e->base + ((uint64_t)e->start_bus << 20); @@ -541,6 +600,8 @@ int pci_stage_discover_report(void) a.ecam = m; a.start_bus = e->start_bus; a.end_bus = e->end_bus; + g_disc_acc[g_disc_acc_n] = a; + g_disc_acc_seg[g_disc_acc_n++] = e->segment; uint32_t n = 0, br = 0; int drc = pci_discover(&a, e->segment, g_disc_found, PCI_MAX_FUNCS, &n, &br); ck_printf("pci_disc: seg %04x bus %02x-%02x functions=%u bridges=%u rc=%d\n", e->segment, e->start_bus, @@ -602,14 +663,14 @@ int pci_nvme_disc_report(const pci_found *f, uint32_t n, const pci_func *bound) uint32_t cnt = 0; const pci_found *c = pci_disc_find_class(f, n, 0x010802u, 0xffffffu, &cnt); if (!c) { - ck_printf("nvme_disc: no class 0x010802 function found by discovery (report-only; bound via segment-0 probe: %s)\n", + ck_printf("nvme_disc: no class 0x010802 function found by discovery (bound: %s)\n", bound ? "different" : "none"); return 0; } const char *rel = !bound ? "none" : (bound->segment == c->segment && bound->bus == c->bus && bound->dev == c->dev && bound->fn == c->fn) ? "same" : "different"; - ck_printf("nvme_disc: candidate seg %04x %02x:%02x.%u bar0=0x%llx decode=%u (report-only; bound via segment-0 probe: %s)\n", + ck_printf("nvme_disc: candidate seg %04x %02x:%02x.%u bar0=0x%llx decode=%u (bound: %s)\n", c->segment, c->bus, c->dev, c->fn, (unsigned long long)found_bar0(c), (c->command >> 1) & 1u, rel); ck_printf("nvme_disc: candidates=%u\n", cnt); return (int)cnt; diff --git a/native/kernel/dev/pci.h b/native/kernel/dev/pci.h index 7951a58e..f7dd9fca 100644 --- a/native/kernel/dev/pci.h +++ b/native/kernel/dev/pci.h @@ -189,6 +189,19 @@ int pci_stage_discover_report(void); * read-only MMIO window report (mmio_window.h). */ const pci_found *pci_stage_disc_found(uint32_t *n); +/* ECAM accessor the last pci_stage_discover_report mapped for `segment` whose + * bus range holds `bus`. PCI_E_ARG when that report mapped none. */ +int pci_stage_disc_access(uint16_t segment, uint8_t bus, pci_bus_access *out); + +/* Turn one discovered endpoint (header type 0) into a pci_func through its + * segment's accessor, so a driver can bind a function outside segment 0 + * (aienos#31, discovery-to-binding cut). Re-reads vendor/device and refuses + * (PCI_E_ARG) if they differ from `f`. Sizes the BARs like pci_enumerate + * (decode turned off around the all-ones writes and restored), so unlike + * pci_discover this WRITES config space of that one function; it never assigns + * a BAR (bars_ok = 0 when a sized BAR is unassigned). */ +int pci_func_from_found(pci_func *out, const pci_found *f, const pci_bus_access *a); + /* NVMe through multi-segment discovery (aienos#31, cut S1). Pure selector over * a discovery result: the first function (lowest segment, then bus, device, * function) whose class_code & mask == class_code & mask of the argument, or @@ -196,7 +209,7 @@ const pci_found *pci_stage_disc_found(uint32_t *n); const pci_found *pci_disc_find_class(const pci_found *f, uint32_t n, uint32_t class_code, uint32_t mask, uint32_t *count); /* Report-only: print the discovery's NVMe (class 0x010802) candidate and - * whether the function the segment-0 probe bound (`bound`, NULL when none) + * whether the function the NVMe bind owns (`bound`, NULL when none) * is the same. Changes no state; returns the number of candidates. */ int pci_nvme_disc_report(const pci_found *f, uint32_t n, const pci_func *bound); diff --git a/native/kernel/stage.mk b/native/kernel/stage.mk index 4ace1a2f..2d2229ef 100644 --- a/native/kernel/stage.mk +++ b/native/kernel/stage.mk @@ -91,7 +91,7 @@ stage-free: $(STAGE_OUT)/stage_all.o # ---- host tests ---------------------------------------------------------- STAGE_HOST_SRCS := $(STAGE_OWN_SRCS) $(STAGE_DIR)/core/ipc.c $(STAGE_NATIVE_SRCS) $(STAGE_NATIVE)/disk/disk_file.c \ $(STAGE_DIR)/svc/tests/ck_host.c $(STAGE_DIR)/svc/tests/stage_test.c -STAGE_HOST_FLAGS := -std=gnu11 -O1 -g -Wall -Wextra -Werror -pthread -DCK_ART_STORE_WRITER +STAGE_HOST_FLAGS := -std=gnu11 -O1 -g -Wall -Wextra -Werror -pthread -DCK_ART_STORE_WRITER -DCK_HOST_TEST STAGE_SAN_FLAGS := -fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer $(STAGE_OUT)/stage_test: $(STAGE_HOST_SRCS) $(wildcard $(STAGE_DIR)/dev/*.h $(STAGE_DIR)/svc/*.h $(STAGE_DIR)/include/*.h) diff --git a/native/kernel/svc/.crumb b/native/kernel/svc/.crumb index 999aeaa8..e94d8ca3 100644 --- a/native/kernel/svc/.crumb +++ b/native/kernel/svc/.crumb @@ -23,21 +23,21 @@ "purpose": "Directory native/kernel/svc/compat. Purpose not yet described by a human." }, { - "digest": "fad382cd7d13229d5c405da8dda0a1e0c654b5964b50e9c606ede401712fa8b6", - "last_commit": "2bc7146", + "digest": "b44c6286f48a561179cc97442ad6fa4c368b53f802bad1b35a493b6a7ed037cf", + "last_commit": "ee2ff16", "name": "tests", "purpose": "Directory native/kernel/svc/tests. Purpose not yet described by a human." } ], - "children_root": "sha256:ef6515417279758e8cad782e4d8d6c2c2208ee9b907764f4a19acf625442611c", - "compiled_at": "2026-10-08T20:55:41Z", + "children_root": "sha256:663de76750aa7914e1994534b4284995ddc98a54574ed3d29e0368506e9a0041", + "compiled_at": "2026-10-09T02:37:36Z", "compiler": "crumb-compile/1", - "digest": "sha256:92746c0561f8e54e2164b2616421fe55acb34bd38aff2461110a437d483b4053", + "digest": "sha256:2712bd906a89792008981a3dc88d1f28f5c4ebcf278751ab852bfd0e76a62e1c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 35, - "generated_at_commit": "2bc7146ee21806d0537c16fe70acabf5f0f6126f", - "source_tree": "sha256:8b83480b0e21d0730d1bdd2352efc6a9f2e3800beb193413248201fc1e65ede8" + "generated_at_commit": "23b2249dd1bbac3314e17b5d70357e71e226cf03", + "source_tree": "sha256:495f8cee8153c2abee8e361c7739463c4e5e5df5a428df16884cd80f56c319c3" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/svc/tests/.crumb b/native/kernel/svc/tests/.crumb index 2404c353..901b913f 100644 --- a/native/kernel/svc/tests/.crumb +++ b/native/kernel/svc/tests/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-08T20:55:41Z", + "compiled_at": "2026-10-09T02:37:36Z", "compiler": "crumb-compile/1", - "digest": "sha256:fad382cd7d13229d5c405da8dda0a1e0c654b5964b50e9c606ede401712fa8b6", + "digest": "sha256:b44c6286f48a561179cc97442ad6fa4c368b53f802bad1b35a493b6a7ed037cf", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 3, - "generated_at_commit": "2bc7146ee21806d0537c16fe70acabf5f0f6126f", - "source_tree": "sha256:e28312bae8bd73464239c186a6bbf6cadbf10220f442af074589059cb612fb9d" + "generated_at_commit": "23b2249dd1bbac3314e17b5d70357e71e226cf03", + "source_tree": "sha256:ed6229a0470e3f5fea11ec069737aac7d38d08cd374479243af9bb19b6aa4389" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/svc/tests/ck_host.c b/native/kernel/svc/tests/ck_host.c index 76fa74cd..52541719 100644 --- a/native/kernel/svc/tests/ck_host.c +++ b/native/kernel/svc/tests/ck_host.c @@ -70,6 +70,13 @@ void ck_panic(const char *fmt, ...) } void *ck_alloc(size_t bytes) { return calloc(1, bytes ? bytes : 1); } void ck_free(void *p) { free(p); } +/* Host: the kernel never frees DMA regions; the shim remembers them and frees at exit so leak checks stay useful. */ +static void *host_dma[32]; +static unsigned host_dma_n; +static void host_dma_free_all(void) +{ + while (host_dma_n) free(host_dma[--host_dma_n]); +} void *ck_dma_alloc(size_t bytes, size_t align, uint64_t *phys) { if (align < 16) align = 16; @@ -77,6 +84,10 @@ void *ck_dma_alloc(size_t bytes, size_t align, uint64_t *phys) void *p = aligned_alloc(align, n); if (!p) return NULL; memset(p, 0, n); + if (host_dma_n < 32) { + if (host_dma_n == 0) atexit(host_dma_free_all); + host_dma[host_dma_n++] = p; + } if (phys) *phys = (uint64_t)(uintptr_t)p; return p; } @@ -200,18 +211,38 @@ int ck_compat_entropy_source(void) { return CK_ENTROPY_HOST; } int ck_entropy_status(void) { return 0; } const char *ck_entropy_reason(void) { return "host-urandom"; } -/* Host: no IORT, so no SMMU (the NVMe gate stays fail-closed). */ +/* Host: no IORT by default, so no SMMU (the NVMe gate stays fail-closed). A test may set + * ck_host_confine_rc = 0 to model an SMMU that confines; every call is recorded. */ +int ck_host_confine_rc = CK_SMMU_ABSENT; +unsigned ck_host_confine_calls, ck_host_unconfine_calls; +uint32_t ck_host_confine_segment, ck_host_confine_rid; +uint64_t ck_host_confine_phys, ck_host_confine_len; int ck_dma_confine(uint32_t segment, uint32_t rid, uint64_t phys, uint64_t len, struct ck_dma_confinement *out) { - (void)segment; (void)rid; (void)phys; (void)len; (void)out; - return CK_SMMU_ABSENT; + ck_host_confine_calls++; + ck_host_confine_segment = segment; + ck_host_confine_rid = rid; + ck_host_confine_phys = phys; + ck_host_confine_len = len; + if (ck_host_confine_rc == 0 && out) { + out->smmu_base = 0x2b400000ull; + out->stream_id = 0x40u | rid; + out->iova = phys; + out->len = len; + } + return ck_host_confine_rc; } int ck_dma_confine_named(const char *acpi_name, uint64_t phys, uint64_t len, struct ck_dma_confinement *out) { (void)acpi_name; (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_unconfine(uint32_t stream_id) +{ + (void)stream_id; + ck_host_unconfine_calls++; + return ck_host_confine_rc == 0 ? 0 : CK_SMMU_EARG; +} int ck_dma_faults(uint32_t stream_id, struct ck_dma_fault *first) { (void)stream_id; (void)first; diff --git a/native/kernel/svc/tests/ck_host.h b/native/kernel/svc/tests/ck_host.h index 1233b31c..e2a52d67 100644 --- a/native/kernel/svc/tests/ck_host.h +++ b/native/kernel/svc/tests/ck_host.h @@ -7,6 +7,10 @@ extern int ck_host_quiet; /* 1: drop ck_printf output */ extern int ck_host_try_map_ok; /* 1: ck_mmio_try_map is identity (default 0: always NULL) */ extern uint64_t ck_host_try_map_fail; /* with try_map_ok: this exact phys still fails (0: none) */ extern int ck_host_unmap_calls; /* successful ck_mmio_unmap_exclusive calls */ +extern int ck_host_confine_rc; /* ck_dma_confine result (default CK_SMMU_ABSENT; 0 models a confining SMMU) */ +extern unsigned ck_host_confine_calls, ck_host_unconfine_calls; +extern uint32_t ck_host_confine_segment, ck_host_confine_rid; +extern uint64_t ck_host_confine_phys, ck_host_confine_len; void ck_host_mmio_reset(void); /* forget every recorded mapping and the counter */ void ck_host_capture_start(void); /* record ck_printf/ck_puts text (even when quiet) */ const char *ck_host_capture_text(void); /* NUL-terminated text captured since start */ diff --git a/native/kernel/svc/tests/stage_test.c b/native/kernel/svc/tests/stage_test.c index 468e75bc..638ab5b3 100644 --- a/native/kernel/svc/tests/stage_test.c +++ b/native/kernel/svc/tests/stage_test.c @@ -649,13 +649,13 @@ static void test_nvme_disc_report(void) ck_host_capture_stop(); const char *x = ck_host_capture_text(); CHECK(has(x, "pci_disc: segments=4 functions=7")); - CHECK(has(x, "nvme_disc: candidate seg 0003 01:00.0 bar0=0x4000000000 decode=1 (report-only; bound via segment-0 probe: none)\n")); - CHECK(has(x, "nvme_disc: candidate seg 0003 01:00.0 bar0=0x4000000000 decode=1 (report-only; bound via segment-0 probe: same)\n")); - CHECK(has(x, "nvme_disc: candidate seg 0003 01:00.0 bar0=0x4000000000 decode=1 (report-only; bound via segment-0 probe: different)\n")); + CHECK(has(x, "nvme_disc: candidate seg 0003 01:00.0 bar0=0x4000000000 decode=1 (bound: none)\n")); + CHECK(has(x, "nvme_disc: candidate seg 0003 01:00.0 bar0=0x4000000000 decode=1 (bound: same)\n")); + CHECK(has(x, "nvme_disc: candidate seg 0003 01:00.0 bar0=0x4000000000 decode=1 (bound: different)\n")); CHECK(!has(x, "seg 0007 01:00.0 bar0=0x5000000000 decode=0 (report-only; bound")); /* second one is counted, not chosen */ CHECK(has(x, "nvme_disc: candidates=2\n")); - CHECK(has(x, "nvme_disc: no class 0x010802 function found by discovery (report-only; bound via segment-0 probe: none)\n")); - CHECK(has(x, "nvme_disc: no class 0x010802 function found by discovery (report-only; bound via segment-0 probe: different)\n")); + CHECK(has(x, "nvme_disc: no class 0x010802 function found by discovery (bound: none)\n")); + CHECK(has(x, "nvme_disc: no class 0x010802 function found by discovery (bound: different)\n")); /* Read-only: fake ECAMs are PROT_READ (a config write would have killed the test) and unchanged. */ CHECK(memcmp(e0, c0, FAKE_BYTES) == 0 && memcmp(e3, c3, FAKE_BYTES) == 0); CHECK(memcmp(e7, c7, FAKE_BYTES) == 0 && memcmp(e15, c15, FAKE_BYTES) == 0); @@ -665,9 +665,13 @@ static void test_nvme_disc_report(void) munmap(e0, FAKE_BYTES); munmap(e3, FAKE_BYTES); munmap(e7, FAKE_BYTES); munmap(e15, FAKE_BYTES); } -/* The devices stage prints the nvme_disc report after the bind, with the - * binding unchanged (the NVMe behind segment 3 is reported; the stage's - * segment-0 probe finds no NVMe, so nothing binds and the report says "none"). */ +extern uint64_t (*pci_host_bar_size)(volatile uint8_t *cfg, uint32_t i); /* CK_HOST_TEST seam in dev/pci.c */ +extern int pci_host_bme_stuck; +static uint64_t emu_bar0_16k(volatile uint8_t *cfg, uint32_t i) { (void)cfg; return i == 0 ? 0x4000u : 0; } + +/* The devices stage binds the NVMe the discovery found behind segment 3 (the segment-0 probe sees none). + * Without an SMMU the DMA gate denies it (fail-closed), ownership is released, and the nvme_disc report + * printed after the bind says "none" owned. */ static void test_nvme_disc_in_stage(void) { uint8_t *e0 = mk_fake_seg0(), *e3 = mk_nvme_seg(0x4000000000ull, 0); /* decode off: the B3 window report must not read the fake BAR */ @@ -680,15 +684,17 @@ static void test_nvme_disc_in_stage(void) ck_host_mcfg = t; ck_host_try_map_ok = 1; ck_host_quiet = 1; + pci_host_bar_size = emu_bar0_16k; ck_host_capture_start(); - CHECK(ck_stage_devices() == 0); + CHECK(ck_stage_devices() == NVME_ESTATE); ck_host_capture_stop(); const char *x = ck_host_capture_text(); const char *d = strstr(x, "nvme_disc: candidate seg 0003 01:00.0"), *v = strstr(x, "devices: pci=ok"); CHECK(d && v && d < v); - CHECK(has(x, "decode=0 (report-only; bound via segment-0 probe: none)\n")); - CHECK(has(x, "bound via segment-0 probe: none)\n")); + CHECK(has(x, "decode=0 (bound: none)\n")); + CHECK(has(x, "dma_gate: nvme denied (NoSmmu), bus master stays off\n")); CHECK(has(x, "devices: pci=ok nvme=unbound")); + pci_host_bar_size = NULL; ck_host_mcfg = NULL; ck_host_try_map_ok = 0; ck_host_quiet = 0; @@ -696,6 +702,186 @@ static void test_nvme_disc_in_stage(void) } +/* ---------------- NVMe bound from the discovery result (aienos#31, discovery-to-binding cut) ---------------- */ + +static uint8_t nv_regs[0x8000] __attribute__((aligned(0x4000))); /* all-zero "controller": VS reads 0 -> NVME_EVS */ +static uint16_t nv_cmd(uint8_t *ecam) { uint8_t *c = cfgp(ecam, 1, 0, 0); return (uint16_t)(c[4] | c[5] << 8); } +static void nv_set_cmd(uint8_t *ecam, uint16_t v) { put16(cfgp(ecam, 1, 0, 0) + 4, v); } + +static void test_nvme_bind_discovery(void) +{ + uint64_t bar0 = (uint64_t)(uintptr_t)nv_regs; + uint8_t *e0 = mk_fake_seg0(), *e3 = mk_nvme_seg(bar0, 0), *e7 = mk_nvme_seg(bar0, 0); + CHECK(e0 && e3 && e7); + if (!e0 || !e3 || !e7) return; + memset(cfgp(e0, 0, 1, 0), 0xff, 4096); /* segment 0 holds no NVMe (GB10 style) */ + nv_set_cmd(e3, 0x4); /* firmware left bus master ON on seg 3 */ + static uint8_t c7[FAKE_BYTES]; + memcpy(c7, e7, FAKE_BYTES); + uint8_t *es[3] = {e0, e7, e3}; + uint16_t ss[3] = {0, 7, 3}; /* table order is not segment order */ + uint8_t t[44 + 16 * 3 + 16]; + mk_n_mcfg(t, 3, es, ss); + ck_host_mcfg = t; + ck_host_try_map_ok = 1; + ck_host_quiet = 1; + pci_host_bar_size = emu_bar0_16k; + CHECK(pci_stage_discover_report() == PCI_OK); + uint32_t nf = 0; + const pci_found *found = pci_stage_disc_found(&nf); + uint32_t ncand = 0; + CHECK(pci_disc_find_class(found, nf, 0x010802, 0xffffff, &ncand) && ncand == 2); /* seg 3 and seg 7; none on seg 0 */ + + /* 1. No SMMU: the NVMe outside segment 0 is found, firmware's bus master is cleared, DMA is denied + * (fail-closed), the other NVMe is never touched. */ + static ck_nvme n; + memset(&n, 0, sizeof n); + ck_host_confine_rc = CK_SMMU_ABSENT; + unsigned calls0 = ck_host_confine_calls; + ck_host_capture_start(); + int rc = ck_nvme_bind(&n, found, nf, NULL); + ck_host_capture_stop(); + const char *x = ck_host_capture_text(); + CHECK(rc == NVME_ESTATE); + CHECK(n.pf == &n.fn && n.fn.segment == 3 && n.fn.bus == 1 && n.fn.dev == 0 && n.fn.fn == 0); + CHECK(n.fn.bar[0].size == 0x4000 && n.fn.bar[0].addr == bar0); + CHECK(has(x, "nvme: 2 class 0x010802 functions in discovery, binding the lowest segment/bus/dev/fn only\n")); + CHECK(has(x, "nvme: firmware left bus master on, cleared before the DMA gate (ok)\n")); + CHECK(has(x, "dma_gate: nvme denied (NoSmmu), bus master stays off\n")); + CHECK(ck_host_confine_calls == calls0 + 1 && !n.confined && !n.bm_on && !n.bound); + CHECK((nv_cmd(e3) & 0x4u) == 0); /* no DMA path without the gate */ + CHECK(memcmp(e7, c7, FAKE_BYTES) == 0); /* the second NVMe was never touched */ + + /* 2. SMMU present: confinement is asked for this function's own segment and requester id with the + * driver's whole DMA region as the only window; bus master comes on after it. Same policy as the + * segment-0 probe had (the DMA code is unchanged). */ + memset(&n, 0, sizeof n); + ck_host_confine_rc = 0; + calls0 = ck_host_confine_calls; + ck_host_capture_start(); + rc = ck_nvme_bind(&n, found, nf, NULL); + ck_host_capture_stop(); + x = ck_host_capture_text(); + CHECK(rc == NVME_EVS); /* host has no controller behind the BAR */ + CHECK(ck_host_confine_calls == calls0 + 1); + CHECK(ck_host_confine_segment == 3 && ck_host_confine_rid == 0x0100u); + CHECK(ck_host_confine_len == (((uint64_t)CK_NVME_DMA_BYTES + NVME_PAGE - 1u) & ~(uint64_t)(NVME_PAGE - 1u))); + CHECK(ck_host_confine_phys != 0 && (ck_host_confine_phys & (NVME_PAGE - 1u)) == 0); + CHECK(n.confined == 1 && n.bm_on == 1 && n.claimed == 1 && n.stream_id == (0x40u | 0x0100u)); + CHECK((nv_cmd(e3) & 0x6u) == 0x6u); /* memory decode + bus master after the grant */ + CHECK(has(x, "dma_gate: nvme granted (Confined), bus master on\n")); + CHECK(memcmp(e7, c7, FAKE_BYTES) == 0); + + /* 3. Double bind: refused, nothing touched (no second confinement, state unchanged). */ + calls0 = ck_host_confine_calls; + ck_host_capture_start(); + int rc2 = ck_nvme_bind(&n, found, nf, NULL); + ck_host_capture_stop(); + x = ck_host_capture_text(); + CHECK(rc2 == NVME_ESTATE); + CHECK(ck_host_confine_calls == calls0); + CHECK(n.confined == 1 && n.bm_on == 1 && n.claimed == 1 && n.fn.segment == 3); + CHECK(has(x, "nvme: bind refused, already owned by seg 0003 01:00.0")); + CHECK(!has(x, "dma_gate:")); + + /* 4. No NVMe in the discovery result: clean refusal, nothing claimed, no DMA gate. */ + static ck_nvme n0; + memset(&n0, 0, sizeof n0); + calls0 = ck_host_confine_calls; + ck_host_capture_start(); + CHECK(ck_nvme_bind(&n0, found, 0, NULL) == -1); + ck_host_capture_stop(); + CHECK(has(ck_host_capture_text(), "nvme: no class 0x010802 function\n")); + CHECK(!n0.claimed && !n0.pf && !n0.bound && ck_host_confine_calls == calls0); + pci_found only_nic[1]; /* non-NVMe class only */ + memset(only_nic, 0, sizeof only_nic); + only_nic[0].class_code = 0x020000; only_nic[0].vendor = 0x8086; + CHECK(ck_nvme_bind(&n0, only_nic, 1, NULL) == -1 && !n0.claimed && ck_host_confine_calls == calls0); + + /* 5. A discovered NVMe whose segment has no mapped ECAM, or whose identity no longer matches the + * ECAM, is refused before DMA is gated. */ + pci_found bad = *pci_disc_find_class(found, nf, 0x010802, 0xffffff, NULL); + bad.segment = 9; + memset(&n0, 0, sizeof n0); + CHECK(ck_nvme_bind(&n0, &bad, 1, NULL) == NVME_EARG && !n0.claimed && ck_host_confine_calls == calls0); + bad = *pci_disc_find_class(found, nf, 0x010802, 0xffffff, NULL); + bad.device ^= 0x0100; + CHECK(ck_nvme_bind(&n0, &bad, 1, NULL) == NVME_EARG && n0.claimed == 0 && !n0.pf && ck_host_confine_calls == calls0); + CHECK(!n0.bm_on && !n0.confined); + /* A refusal before any device write leaves no owner: a later bind of a good fixture on the same state works. */ + ck_host_confine_rc = 0; + memset(&n, 0, sizeof n); + ck_host_capture_start(); + CHECK(ck_nvme_bind(&n0, found, nf, NULL) == NVME_EVS && n0.claimed == 1 && n0.confined == 1 && ck_host_confine_calls == calls0 + 1); + ck_host_capture_stop(); + CHECK(has(ck_host_capture_text(), "dma_gate: nvme granted (Confined), bus master on\n")); + calls0 = ck_host_confine_calls; + + /* 5b. Refusal AFTER a device write (bus master will not clear): stays owned (fail closed), rebind refused. */ + static ck_nvme ns; + memset(&ns, 0, sizeof ns); + nv_set_cmd(e3, 0x4); + pci_host_bme_stuck = 1; + ck_host_capture_start(); + CHECK(ck_nvme_bind(&ns, found, nf, NULL) == NVME_ESTATE); + CHECK(ns.claimed == 1 && !ns.bm_on && !ns.confined && ck_host_confine_calls == calls0); + CHECK(ck_nvme_bind(&ns, found, nf, NULL) == NVME_ESTATE && ck_host_confine_calls == calls0); + ck_host_capture_stop(); + x = ck_host_capture_text(); + CHECK(has(x, "it stays owned, fail closed")); + CHECK(has(x, "nvme: bind refused, already owned by seg 0003 01:00.0")); + pci_host_bme_stuck = 0; + + /* 6. Whole devices stage: a bind that fails after the grant releases everything (bus master off, stream + * back to abort) and gives ownership back, so the next stage run can bind again. */ + nv_set_cmd(e3, 0); + ck_host_confine_rc = 0; + calls0 = ck_host_confine_calls; + unsigned un0 = ck_host_unconfine_calls; + ck_host_capture_start(); + CHECK(ck_stage_devices() == NVME_EVS); + ck_host_capture_stop(); + x = ck_host_capture_text(); + CHECK(ck_host_confine_calls == calls0 + 1 && ck_host_unconfine_calls == un0 + 1); + CHECK(ck_host_confine_segment == 3 && ck_host_confine_rid == 0x0100u); + CHECK((nv_cmd(e3) & 0x4u) == 0); + CHECK(has(x, "dma_gate: nvme bus master revoked\n") && has(x, "returned to abort")); + CHECK(has(x, "devices: pci=ok nvme=unbound")); + CHECK(has(x, "nvme_disc: candidate seg 0003 01:00.0") && has(x, "(bound: none)\n")); + CHECK(ck_stage_devices() == NVME_EVS); /* ownership was returned: bound again, once */ + CHECK(ck_host_confine_calls == calls0 + 2 && ck_host_unconfine_calls == un0 + 2); + + /* 7. Stage level, firmware bus master that will not clear. The bind refuses (fail closed) and the stage's + * release must NOT hand the owner back: the device was written and may still be a bus master. A second + * stage run is refused as "already owned" and its refusal must not tear anything down; only an operator + * reset ends this (so this case runs last). */ + nv_set_cmd(e3, 0x4); + pci_host_bme_stuck = 1; + ck_host_confine_rc = 0; + calls0 = ck_host_confine_calls; + un0 = ck_host_unconfine_calls; + ck_host_capture_start(); + CHECK(ck_stage_devices() == NVME_ESTATE); + CHECK(ck_host_confine_calls == calls0 && ck_host_unconfine_calls == un0); /* never reached the DMA gate */ + CHECK(ck_stage_devices() == NVME_ESTATE); /* owner still there: refused */ + CHECK(ck_stage_devices() == NVME_ESTATE); /* and the refusal did not drop it */ + ck_host_capture_stop(); + x = ck_host_capture_text(); + CHECK(ck_host_confine_calls == calls0 && ck_host_unconfine_calls == un0); + CHECK(has(x, "it stays owned, fail closed")); + CHECK(has(x, "nvme: bind refused, already owned by seg 0003 01:00.0")); + CHECK(!has(x, "dma_gate: nvme granted")); + pci_host_bme_stuck = 0; + + pci_host_bar_size = NULL; + ck_host_confine_rc = CK_SMMU_ABSENT; + ck_host_mcfg = NULL; + ck_host_try_map_ok = 0; + ck_host_quiet = 0; + munmap(e0, FAKE_BYTES); munmap(e3, FAKE_BYTES); munmap(e7, FAKE_BYTES); +} + + /* ---------------- Store boot ---------------- */ #define T_UNITS 128u @@ -1942,6 +2128,7 @@ int main(void) test_pci_disc_find_class(); test_nvme_disc_report(); test_nvme_disc_in_stage(); + test_nvme_bind_discovery(); test_mmio_window(); test_mmio_unmap(); test_mmio_stage_report();