aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / dev / nvme_bind.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* nvme_bind.c -- NVMe binding through ck.h (see nvme_bind.h). Freestanding. */ |
| 18 | #include "nvme_bind.h" |
| 19 | #include "nvme_shutdown.h" |
| 20 | #include "ck.h" |
| 21 | #include "disk_layout.h" |
| 22 | |
| 23 | /* NVMe DMA rule: no SMMU confinement means no DMA. The default build asks |
| 24 | * the core for SMMU confinement (ck_dma_confine) and refuses NVMe DMA when |
| 25 | * there is no SMMU or it did not come up (fail-closed). The unconfined |
| 26 | * bypass is TEST-ONLY: it exists only for QEMU without an SMMU, only when a |
| 27 | * QEMU gate script asks for it with CK_QEMU_UNSAFE_DMA=1, and is never used |
| 28 | * when an SMMU is present; it can never be combined with a hardware-staging |
| 29 | * build (same rule as the compile_error! in crates/aienos-boot/src/lib.rs). */ |
| 30 | #if defined(CK_NVME_DMA_BYPASS) |
| 31 | #error "CK_NVME_DMA_BYPASS is retired: the unconfined NVMe DMA bypass is CK_QEMU_UNSAFE_DMA=1 (QEMU only)" |
| 32 | #endif |
| 33 | #if defined(CK_QEMU_UNSAFE_DMA) && defined(CK_HARDWARE_STAGING) |
| 34 | #error "CK_QEMU_UNSAFE_DMA (unconfined DMA, QEMU debug only) cannot be combined with CK_HARDWARE_STAGING: no SMMU confinement means no DMA" |
| 35 | #endif |
| 36 | #if defined(CK_QEMU_UNSAFE_DMA) && CK_QEMU_UNSAFE_DMA == 1 |
| 37 | #define CK_NVME_UNSAFE_BYPASS 1 |
| 38 | #elif defined(CK_QEMU_UNSAFE_DMA) && CK_QEMU_UNSAFE_DMA != 0 |
| 39 | #error "CK_QEMU_UNSAFE_DMA must be 0 or 1" |
| 40 | #else |
| 41 | #define CK_NVME_UNSAFE_BYPASS 0 |
| 42 | #endif |
| 43 | |
| 44 | static uint32_t m_r32(void *ctx, uint32_t off) |
| 45 | { |
| 46 | ck_nvme *n = ctx; |
| 47 | return *(volatile uint32_t *)(n->bar0 + off); |
| 48 | } |
| 49 | static void m_w32(void *ctx, uint32_t off, uint32_t v) |
| 50 | { |
| 51 | ck_nvme *n = ctx; |
| 52 | *(volatile uint32_t *)(n->bar0 + off) = v; |
| 53 | } |
| 54 | static void m_barrier(void *ctx) |
| 55 | { |
| 56 | (void)ctx; |
| 57 | ck_mb(); |
| 58 | } |
| 59 | static void m_delay(void *ctx, uint32_t us) |
| 60 | { |
| 61 | (void)ctx; |
| 62 | ck_udelay(us); |
| 63 | } |
| 64 | |
| 65 | static uint8_t probe_buf[CK_LAYOUT_UNIT]; |
| 66 | static uint8_t probe_rd[CK_LAYOUT_UNIT]; |
| 67 | static uint8_t probe_orig[CK_LAYOUT_UNIT]; |
| 68 | |
| 69 | /* SMMU negative test (confined mode only). The driver's bounce-buffer bus |
| 70 | * address is pointed at a pattern-filled page that is NOT in the stream's |
| 71 | * window and a one-block read of LBA 0 is issued, so the controller tries |
| 72 | * to DMA into that page. Pass: the SMMU records a translation fault for this |
| 73 | * stream at that page, the page keeps its pattern (the write never landed), |
| 74 | * and after restoring the bounce address a normal read works again. The |
| 75 | * read's own status is reported but not judged (QEMU may complete it). */ |
| 76 | static int smmu_negative_test(ck_nvme *n) |
| 77 | { |
| 78 | uint64_t cphys = 0; |
| 79 | volatile uint8_t *canary = ck_dma_alloc(NVME_PAGE, NVME_PAGE, &cphys); |
| 80 | if (!canary) { |
| 81 | ck_printf("smmu_negative: FAIL (no canary page)\n"); |
| 82 | return -1; |
| 83 | } |
| 84 | for (uint32_t i = 0; i < NVME_PAGE; i++) |
| 85 | canary[i] = 0x5a; |
| 86 | (void)ck_dma_faults(n->stream_id, 0); /* drain anything older */ |
| 87 | nvme_ctrl *c = &n->ctrl; |
| 88 | uint64_t saved = c->bounce_phys; |
| 89 | c->bounce_phys = cphys; |
| 90 | ck_mb(); |
| 91 | int rc = disk_read(&n->disk, 0, 1, probe_rd); |
| 92 | c->bounce_phys = saved; |
| 93 | ck_mb(); |
| 94 | struct ck_dma_fault fl = { 0, 0, 0, 0 }; |
| 95 | int faults = ck_dma_faults(n->stream_id, &fl); |
| 96 | int intact = 1; |
| 97 | for (uint32_t i = 0; i < NVME_PAGE; i++) |
| 98 | if (canary[i] != 0x5a) intact = 0; |
| 99 | int rc2 = disk_read(&n->disk, 0, 1, probe_rd); |
| 100 | int ok = faults >= 1 && fl.type == CK_DMA_FAULT_TRANSLATION && fl.stream_id == n->stream_id && |
| 101 | (fl.addr & ~(uint64_t)(NVME_PAGE - 1u)) == cphys && intact && rc2 == 0; |
| 102 | ck_printf("smmu_negative: %s dma outside window iova=0x%llx faults=%d type=0x%x sid=0x%x addr=0x%llx " |
| 103 | "page=%s cmd_rc=%d recovery_read=%s\n", |
| 104 | ok ? "refused" : "FAIL", (unsigned long long)cphys, faults, fl.type, fl.stream_id, |
| 105 | (unsigned long long)fl.addr, intact ? "intact" : "MODIFIED", rc, rc2 ? "FAIL" : "ok"); |
| 106 | return ok ? 0 : -1; |
| 107 | } |
| 108 | |
| 109 | int ck_disk_rw_probe(const disk_dev *d, uint32_t seed, uint64_t *lba_out) |
| 110 | { |
| 111 | uint32_t bpu = CK_LAYOUT_UNIT / d->block_size; |
| 112 | uint64_t units = d->block_count / bpu; |
| 113 | if (units < 1) return DISK_EGEOMETRY; |
| 114 | uint64_t lba = (units - 1u) * bpu; |
| 115 | if (lba_out) *lba_out = lba; |
| 116 | int rc = disk_read(d, lba, bpu, probe_orig); |
| 117 | if (rc) return rc; |
| 118 | uint32_t x = seed ? seed : 0x9e3779b9u; |
| 119 | for (uint32_t i = 0; i < CK_LAYOUT_UNIT; i++) { |
| 120 | x ^= x << 13; |
| 121 | x ^= x >> 17; |
| 122 | x ^= x << 5; |
| 123 | probe_buf[i] = (uint8_t)x; |
| 124 | probe_rd[i] = 0; |
| 125 | } |
| 126 | rc = disk_write(d, lba, bpu, probe_buf); |
| 127 | if (rc) return rc; |
| 128 | rc = disk_flush(d); |
| 129 | if (rc) return rc; |
| 130 | rc = disk_read(d, lba, bpu, probe_rd); |
| 131 | if (rc) return rc; |
| 132 | int match = 1; |
| 133 | for (uint32_t i = 0; i < CK_LAYOUT_UNIT; i++) |
| 134 | if (probe_rd[i] != probe_buf[i]) match = 0; |
| 135 | /* Put the original bytes back, durably, and verify them. */ |
| 136 | rc = disk_write(d, lba, bpu, probe_orig); |
| 137 | if (rc) return rc; |
| 138 | rc = disk_flush(d); |
| 139 | if (rc) return rc; |
| 140 | rc = disk_read(d, lba, bpu, probe_rd); |
| 141 | if (rc) return rc; |
| 142 | for (uint32_t i = 0; i < CK_LAYOUT_UNIT; i++) |
| 143 | if (probe_rd[i] != probe_orig[i]) return DISK_EIO; |
| 144 | return match ? DISK_OK : DISK_EIO; |
| 145 | } |
| 146 | |
| 147 | int ck_nvme_bind(ck_nvme *n, const pci_found *found, uint32_t nfound, const pci_system *probe) |
| 148 | { |
| 149 | /* Ownership first: one owner at a time. A refused call changes nothing. */ |
| 150 | if (n->claimed) { |
| 151 | ck_printf("nvme: bind refused, already owned by seg %04x %02x:%02x.%u (bound=%d bm_on=%d confined=%d)\n", |
| 152 | n->fn.segment, n->fn.bus, n->fn.dev, n->fn.fn, n->bound, n->bm_on, n->confined); |
| 153 | return NVME_ESTATE; |
| 154 | } |
| 155 | n->bound = 0; |
| 156 | n->bm_on = 0; |
| 157 | n->confined = 0; |
| 158 | n->shut = 0; |
| 159 | n->stuck = 0; |
| 160 | n->stream_id = 0; |
| 161 | n->pf = 0; |
| 162 | #if CK_NVME_UNSAFE_BYPASS |
| 163 | ck_printf("WARNING: UNSAFE NVME DMA BYPASS BUILD (CK_QEMU_UNSAFE_DMA=1, QEMU debug only, TEST-ONLY)\n"); |
| 164 | #endif |
| 165 | #if defined(CK_TEST_DISK_XLATE_BYPASS) |
| 166 | ck_printf("WARNING: TEST-ONLY DISK TRANSLATION BYPASS BUILD (CK_TEST_DISK_XLATE_BYPASS=1, DISK_LAYOUT " |
| 167 | "mutation; never counts toward a PASS)\n"); |
| 168 | #endif |
| 169 | uint32_t cands = 0; |
| 170 | const pci_found *cand = pci_disc_find_class(found, nfound, 0x010802u, 0xffffffu, &cands); |
| 171 | if (!cand) { |
| 172 | ck_printf("nvme: no class 0x010802 function\n"); |
| 173 | return -1; |
| 174 | } |
| 175 | pci_bus_access acc; |
| 176 | if (pci_stage_disc_access(cand->segment, cand->bus, &acc)) { |
| 177 | ck_printf("nvme: unavailable (no mapped ECAM for seg %04x bus %02x)\n", cand->segment, cand->bus); |
| 178 | return NVME_EARG; |
| 179 | } |
| 180 | n->claimed = 1; /* recorded once, before the device is touched */ |
| 181 | if (pci_func_from_found(&n->fn, cand, &acc)) { |
| 182 | /* Refused before any write to the device: as if the bind was never attempted. */ |
| 183 | n->claimed = 0; |
| 184 | ck_printf("nvme: unavailable (seg %04x %02x:%02x.%u cannot be read as an endpoint; nothing written, not owned)\n", |
| 185 | cand->segment, cand->bus, cand->dev, cand->fn); |
| 186 | return NVME_EARG; |
| 187 | } |
| 188 | if (probe) /* keep the log wording for a function the segment-0 stage assigned */ |
| 189 | for (uint32_t i = 0; i < probe->n; i++) { |
| 190 | const pci_func *p = &probe->f[i]; |
| 191 | if (p->segment == n->fn.segment && p->bus == n->fn.bus && p->dev == n->fn.dev && p->fn == n->fn.fn) |
| 192 | for (uint32_t b = 0; b < PCI_MAX_BARS; b++) n->fn.bar[b].assigned_here = p->bar[b].assigned_here; |
| 193 | } |
| 194 | const pci_func *f = n->pf = &n->fn; |
| 195 | if (cands > 1) |
| 196 | ck_printf("nvme: %u class 0x010802 functions in discovery, binding the lowest segment/bus/dev/fn only\n", cands); |
| 197 | /* A function firmware left with bus mastering on must not be live before the DMA gate. */ |
| 198 | if (cand->command & 0x4u) { |
| 199 | int brc = pci_bus_master_off(f); |
| 200 | ck_printf("nvme: firmware left bus master on, cleared before the DMA gate (%s)\n", brc ? "STUCK" : "ok"); |
| 201 | if (brc) { |
| 202 | n->stuck = 1; /* release keeps the owner; only a reset ends it */ |
| 203 | ck_printf("nvme: unavailable (bus master would not clear; the device was written, so it stays owned, fail closed)\n"); |
| 204 | return NVME_ESTATE; |
| 205 | } |
| 206 | } |
| 207 | const pci_bar *b = &f->bar[0]; |
| 208 | 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, |
| 209 | f->fn, f->vendor, f->device, (unsigned long long)b->addr, (unsigned long long)b->size, |
| 210 | b->assigned_here ? " (assigned by stage)" : " (firmware)"); |
| 211 | if (b->io || b->addr == 0 || b->size < 0x2000u) { |
| 212 | ck_printf("nvme: unavailable (BAR0 unusable)\n"); |
| 213 | return NVME_EARG; |
| 214 | } |
| 215 | /* DMA gate (same policy as the Rust dma_gate::dma_grant): SMMU |
| 216 | * confinement first; with no SMMU at all, only the QEMU-only unsafe |
| 217 | * bypass build may enable DMA; an SMMU that is present but did not come |
| 218 | * up never falls back to the bypass. The DMA region is allocated first |
| 219 | * so the confinement window is exactly the region the driver uses. */ |
| 220 | uint64_t phys = 0; |
| 221 | uint8_t *dma = ck_dma_alloc(CK_NVME_DMA_BYTES, NVME_PAGE, &phys); |
| 222 | if (!dma) { |
| 223 | ck_printf("nvme: unavailable (DMA allocation of %u bytes failed)\n", (unsigned)CK_NVME_DMA_BYTES); |
| 224 | return NVME_EARG; |
| 225 | } |
| 226 | uint32_t rid = ((uint32_t)f->bus << 8) | ((uint32_t)f->dev << 3) | (uint32_t)f->fn; |
| 227 | uint64_t win = ((uint64_t)CK_NVME_DMA_BYTES + NVME_PAGE - 1u) & ~(uint64_t)(NVME_PAGE - 1u); |
| 228 | struct ck_dma_confinement cf; |
| 229 | int src = ck_dma_confine(f->segment, rid, phys, win, &cf); |
| 230 | if (src == 0) { |
| 231 | n->confined = 1; |
| 232 | n->stream_id = cf.stream_id; |
| 233 | ck_printf("smmu: enabled base=0x%llx stream_id=0x%x\n", (unsigned long long)cf.smmu_base, cf.stream_id); |
| 234 | ck_printf("smmu_dma_window: nvme only, translation active iova=0x%llx len=0x%llx rid=0x%x\n", |
| 235 | (unsigned long long)cf.iova, (unsigned long long)cf.len, rid); |
| 236 | ck_printf("dma_gate: nvme granted (Confined), bus master on\n"); |
| 237 | } else if (src == CK_SMMU_ABSENT) { |
| 238 | #if CK_NVME_UNSAFE_BYPASS |
| 239 | ck_printf("WARNING: UNSAFE NVME DMA BYPASS ACTIVE (no SMMU on this machine; device can DMA anywhere; TEST-ONLY build)\n"); |
| 240 | ck_printf("dma_gate: nvme granted (UnsafeBypass), bus master on\n"); |
| 241 | #else |
| 242 | ck_printf("dma_gate: nvme denied (NoSmmu), bus master stays off\n"); |
| 243 | ck_printf("nvme: unavailable (SMMU DMA isolation not active)\n"); |
| 244 | return NVME_ESTATE; |
| 245 | #endif |
| 246 | } else { |
| 247 | ck_printf("dma_gate: nvme denied (SmmuNotReady rc=%d), bus master stays off\n", src); |
| 248 | ck_printf("nvme: unavailable (SMMU DMA isolation not active)\n"); |
| 249 | return NVME_ESTATE; |
| 250 | } |
| 251 | n->bar0 = (volatile uint8_t *)ck_mmio_map(b->addr, (size_t)b->size); |
| 252 | pci_enable(f, 1); |
| 253 | n->bm_on = 1; |
| 254 | nvme_ops ops = {n, m_r32, m_w32, 0, 0, m_barrier, m_delay}; |
| 255 | nvme_dma region = {dma, phys, CK_NVME_DMA_BYTES}; |
| 256 | nvme_config cfg = {0, 0}; |
| 257 | int rc = nvme_init(&n->ctrl, &ops, ®ion, &cfg); |
| 258 | if (rc) { |
| 259 | ck_printf("nvme: identify FAIL (init rc=%d last_err=%d)\n", rc, n->ctrl.last_err); |
| 260 | return rc; |
| 261 | } |
| 262 | nvme_ctrl *c = &n->ctrl; |
| 263 | ck_printf("nvme: identify ok vs=0x%08x nn=%u nsid=%u mdts=%u vwc=%u max_xfer=%u\n", c->vs, c->nn, c->nsid, |
| 264 | c->mdts, c->vwc, c->max_xfer_bytes); |
| 265 | ck_printf("nvme: geometry nsid=%u block_count=%llu block_size=%u\n", c->nsid, |
| 266 | (unsigned long long)c->block_count, c->block_size); |
| 267 | ck_printf("nvme: atomicity block_size=%u awupf_raw=%u nawupf_raw=%u nabspf_raw=%u nabo_blocks=%u\n", |
| 268 | c->block_size, c->awupf_raw, c->nawupf_raw, c->nabspf_raw, c->nabo_blocks); |
| 269 | rc = nvme_disk(c, &n->disk); |
| 270 | if (rc) { |
| 271 | ck_printf("nvme: disk_dev refused rc=%d\n", rc); |
| 272 | return rc; |
| 273 | } |
| 274 | /* Bounds: a read past the namespace end is refused before any command. */ |
| 275 | uint8_t *one = probe_rd; |
| 276 | rc = disk_read(&n->disk, c->block_count, 1, one); |
| 277 | ck_printf("nvme: bounds read lba=%llu -> %s (rc=%d)\n", (unsigned long long)c->block_count, |
| 278 | rc == DISK_ERANGE ? "refused" : "NOT REFUSED", rc); |
| 279 | if (rc != DISK_ERANGE) return DISK_EIO; |
| 280 | rc = disk_read(&n->disk, 0, 1, one); |
| 281 | ck_printf("nvme: read lba=0 blocks=1 %s (rc=%d)\n", rc ? "FAIL" : "ok", rc); |
| 282 | if (rc) return rc; |
| 283 | /* Partition-aware footprint (dev/disk_part.h, audit R2): the GPT is |
| 284 | * parsed read-only; without exactly one valid AIENOS partition nothing is |
| 285 | * ever written to this disk and there is no whole-disk fallback. */ |
| 286 | ck_gpt_info gi; |
| 287 | rc = ck_gpt_find_aienos(&n->disk, &n->part, &gi); |
| 288 | if (rc) { |
| 289 | ck_printf("disk: no AIENOS partition, refusing writes (gpt: %s, rc=%d)\n", ck_gpt_strerror(rc), rc); |
| 290 | return rc; |
| 291 | } |
| 292 | ck_printf("disk: gpt ok primary+backup crc32 entries=%u used=%u aienos_index=%u first_lba=%llu " |
| 293 | "last_lba=%llu blocks=%llu\n", |
| 294 | gi.entries, gi.used, gi.part_index, (unsigned long long)n->part.first_lba, |
| 295 | (unsigned long long)(n->part.first_lba + n->part.blocks - 1u), (unsigned long long)n->part.blocks); |
| 296 | /* The translation layer itself (no I/O): partition LBA 0 maps to the |
| 297 | * partition start; past the end, straddling the end and an overflowing |
| 298 | * LBA are refused. Any of these not refused: stop before any write. */ |
| 299 | uint64_t a0 = 0, ax = 0; |
| 300 | int x0 = ck_part_xlate(&n->part, 0, 1, &a0); |
| 301 | int x1 = ck_part_xlate(&n->part, n->part.blocks, 1, &ax); |
| 302 | int x2 = ck_part_xlate(&n->part, n->part.blocks - 1u, 2, &ax); |
| 303 | int x3 = ck_part_xlate(&n->part, ~(uint64_t)0, 1, &ax); |
| 304 | int xok = x0 == DISK_OK && x1 == DISK_ERANGE && x2 == DISK_ERANGE && x3 == DISK_ERANGE; |
| 305 | ck_printf("disk: xlate part_lba=0 -> disk_lba=%llu (rc=%d)\n", (unsigned long long)a0, x0); |
| 306 | ck_printf("disk: write past partition end part_lba=%llu -> %s (xlate rc=%d,%d,%d)\n", (unsigned long long)n->part.blocks, |
| 307 | xok ? "refused" : "NOT REFUSED", x1, x2, x3); |
| 308 | if (!xok) return DISK_ERANGE; /* not -1: devices.c reads -1 as "no NVMe present" */ |
| 309 | uint64_t lba = 0, dlba = 0; |
| 310 | rc = ck_disk_rw_probe(&n->part.dev, (uint32_t)ck_time_us() ^ 0xa5a5a5a5u, &lba); |
| 311 | int xr = ck_part_xlate(&n->part, lba, CK_LAYOUT_UNIT / n->part.dev.block_size, &dlba); |
| 312 | ck_printf("nvme: rw probe part_lba=%llu disk_lba=%llu bytes=%u write+flush+readback %s (rc=%d)\n", |
| 313 | (unsigned long long)lba, (unsigned long long)dlba, CK_LAYOUT_UNIT, rc ? "FAIL" : "match", rc); |
| 314 | if (rc) return rc; |
| 315 | if (xr) return DISK_ERANGE; |
| 316 | if (n->confined) { |
| 317 | rc = smmu_negative_test(n); |
| 318 | if (rc) return DISK_EIO; |
| 319 | } |
| 320 | n->bound = 1; |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | int ck_nvme_shutdown_bound(ck_nvme *n) |
| 325 | { |
| 326 | if (!n || !n->bar0 || !n->bm_on || n->shut) |
| 327 | return CK_NVME_SHUT_EARG; |
| 328 | n->shut = 1; |
| 329 | struct ck_nvme_shut_ops o = {n, m_r32, m_w32, m_barrier, m_delay}; |
| 330 | struct ck_nvme_shut_result r = {0, 0, 0, 0}; |
| 331 | int rc = ck_nvme_shutdown(&o, CK_NVME_SHUT_TIMEOUT_US, &r); |
| 332 | ck_printf("nvme: shutdown normal cc=0x%08x->0x%08x csts=0x%08x shst=%s waited_us=%u\n", r.cc_before, |
| 333 | r.cc_after, r.csts, ck_nvme_shutdown_str(rc), r.waited_us); |
| 334 | if (ck_nvme_shutdown_needs_disable(rc)) { |
| 335 | /* The controller may still be live: stop it (CC.EN = 0, wait for |
| 336 | * CSTS.RDY = 0) before the caller cuts bus mastering. */ |
| 337 | struct ck_nvme_shut_result d = {0, 0, 0, 0}; |
| 338 | int drc = ck_nvme_disable(&o, CK_NVME_DISABLE_TIMEOUT_US, &d); |
| 339 | ck_printf("nvme: shutdown fallback disable cc=0x%08x->0x%08x csts=0x%08x rdy0=%s waited_us=%u\n", |
| 340 | d.cc_before, d.cc_after, d.csts, ck_nvme_shutdown_str(drc), d.waited_us); |
| 341 | } |
| 342 | return rc; |
| 343 | } |