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 / net_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 | /* net_bind.c -- virtio-net binding and UDP round trip (see net_bind.h). |
| 18 | * Freestanding. */ |
| 19 | #include "net_bind.h" |
| 20 | #include "ck.h" |
| 21 | #include "net_udp.h" |
| 22 | #include <string.h> |
| 23 | |
| 24 | #define WIN_N 4 |
| 25 | #define ARP_TRIES 4u |
| 26 | #define ARP_WAIT_US 500000u |
| 27 | #define UDP_TRIES 5u |
| 28 | #define UDP_WAIT_US 1000000u |
| 29 | #define RESET_WAIT_US 100000u |
| 30 | #define PROBE_WAIT_US 3000000u |
| 31 | #define CANARY_BYTE 0x5au |
| 32 | |
| 33 | typedef struct { |
| 34 | const pci_func *pf; |
| 35 | volatile uint8_t *bar_va[PCI_MAX_BARS]; |
| 36 | volatile uint8_t *win[WIN_N]; |
| 37 | vnet_dev dev; |
| 38 | int bm_on, confined; |
| 39 | uint32_t stream_id; |
| 40 | } ck_vnet; |
| 41 | |
| 42 | static ck_vnet g_net; |
| 43 | /* ck_dma_alloc memory is never freed: allocate the shared region once and |
| 44 | * reuse it on a later bind. */ |
| 45 | static uint8_t *g_dma_mem; |
| 46 | static uint64_t g_dma_phys; |
| 47 | static uint8_t frame[VNET_FRAME_MAX]; |
| 48 | static uint8_t rxbuf[VNET_FRAME_MAX]; |
| 49 | |
| 50 | static uint32_t o_read(void *ctx, vnet_window w, uint32_t off, unsigned width) |
| 51 | { |
| 52 | volatile uint8_t *p = ((ck_vnet *)ctx)->win[w] + off; |
| 53 | if (width == 1) return *p; |
| 54 | if (width == 2) return *(volatile uint16_t *)p; |
| 55 | return *(volatile uint32_t *)p; |
| 56 | } |
| 57 | static void o_write(void *ctx, vnet_window w, uint32_t off, unsigned width, uint32_t v) |
| 58 | { |
| 59 | volatile uint8_t *p = ((ck_vnet *)ctx)->win[w] + off; |
| 60 | if (width == 1) *p = (uint8_t)v; |
| 61 | else if (width == 2) *(volatile uint16_t *)p = (uint16_t)v; |
| 62 | else *(volatile uint32_t *)p = v; |
| 63 | ck_mb(); |
| 64 | } |
| 65 | static void o_notify(void *ctx, uint32_t off, uint16_t q) |
| 66 | { |
| 67 | ck_mb(); /* ring writes visible before the doorbell */ |
| 68 | *(volatile uint16_t *)(((ck_vnet *)ctx)->win[VNET_WIN_NOTIFY] + off) = q; |
| 69 | } |
| 70 | |
| 71 | /* Map the BAR behind one capability window; 0 on a bad BAR or a window |
| 72 | * outside it. */ |
| 73 | static volatile uint8_t *map_window(ck_vnet *n, const virtio_pci_region *r) |
| 74 | { |
| 75 | if (!r->present || r->bar >= PCI_MAX_BARS) return 0; |
| 76 | const pci_bar *b = &n->pf->bar[r->bar]; |
| 77 | if (b->io || b->addr == 0 || b->size == 0) return 0; |
| 78 | if ((uint64_t)r->offset + r->length > b->size) return 0; |
| 79 | if (!n->bar_va[r->bar]) n->bar_va[r->bar] = (volatile uint8_t *)ck_mmio_map(b->addr, (size_t)b->size); |
| 80 | return n->bar_va[r->bar] + r->offset; |
| 81 | } |
| 82 | |
| 83 | static void hex8(uint32_t v, char *o) |
| 84 | { |
| 85 | static const char h[] = "0123456789abcdef"; |
| 86 | for (int i = 7; i >= 0; i--, v >>= 4) o[i] = h[v & 15u]; |
| 87 | } |
| 88 | |
| 89 | static int has_prefix(const uint8_t *b, size_t n, const char *s, size_t sn) |
| 90 | { |
| 91 | return n >= sn && memcmp(b, s, sn) == 0; |
| 92 | } |
| 93 | static int contains(const uint8_t *b, size_t n, const char *s, size_t sn) |
| 94 | { |
| 95 | for (size_t i = 0; i + sn <= n; i++) |
| 96 | if (memcmp(b + i, s, sn) == 0) return 1; |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | int ck_net_live(void) { return g_net.pf && (g_net.bm_on || g_net.confined); } |
| 101 | |
| 102 | void ck_net_release(void) |
| 103 | { |
| 104 | ck_vnet *n = &g_net; |
| 105 | if (!n->pf) return; |
| 106 | if (n->bm_on) { |
| 107 | /* Device reset (status 0) stops the queues; then cut bus mastering. */ |
| 108 | uint32_t st = 0xffu; |
| 109 | if (n->win[VNET_WIN_COMMON]) { |
| 110 | o_write(n, VNET_WIN_COMMON, VNET_CC_STATUS, 1, 0); |
| 111 | uint64_t t0 = ck_time_us(); |
| 112 | while ((st = o_read(n, VNET_WIN_COMMON, VNET_CC_STATUS, 1)) != 0 && ck_time_us() - t0 < RESET_WAIT_US) |
| 113 | ck_udelay(10); |
| 114 | } |
| 115 | ck_printf("virtio_net: device reset status=0x%02x (%s)\n", st, st == 0 ? "stopped" : "TIMEOUT"); |
| 116 | if (pci_bus_master_off(n->pf) == 0) { |
| 117 | n->bm_on = 0; |
| 118 | ck_printf("dma_gate: virtio_net bus master revoked\n"); |
| 119 | } else |
| 120 | ck_printf("dma_gate: virtio_net bus master revoke FAILED (command register still has BME)\n"); |
| 121 | } |
| 122 | if (n->confined) { |
| 123 | /* A failed abort leaves the stream live in the SMMU service: it stays |
| 124 | * confined (ck_net_live stays 1) and a later release retries. */ |
| 125 | int urc = ck_dma_unconfine(n->stream_id); |
| 126 | if (urc == 0) n->confined = 0; |
| 127 | ck_printf("smmu: virtio_net stream 0x%x %s (rc=%d%s%s)\n", n->stream_id, |
| 128 | urc == 0 ? "returned to abort" : "abort FAILED, stays confined", urc, urc ? ": " : "", |
| 129 | urc ? ck_dma_unconfine_why(urc) : ""); |
| 130 | if (urc != 0) ck_dma_fault_report("virtio_net", n->stream_id); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /* Poll RX until `want` arrives or `us` passes. Answers ARP requests for our |
| 135 | * address on the way. Returns the kind seen (CK_NET_IGNORED on timeout), or |
| 136 | * -1 when the device is broken. */ |
| 137 | static int wait_for(ck_vnet *n, ck_net_peer *p, ck_net_kind want, uint32_t us, size_t *len_out, int *csum, |
| 138 | unsigned *frames) |
| 139 | { |
| 140 | uint64_t t0 = ck_time_us(); |
| 141 | do { |
| 142 | size_t len = 0; |
| 143 | int got = 0; |
| 144 | vnet_err e = vnet_rx(&n->dev, rxbuf, sizeof rxbuf, &len, &got); |
| 145 | if (e != VNET_OK) { |
| 146 | ck_printf("net: rx error %s\n", vnet_err_name(e)); |
| 147 | return -1; |
| 148 | } |
| 149 | (void)vnet_tx_reclaim(&n->dev, 0); |
| 150 | if (!got) continue; |
| 151 | (*frames)++; |
| 152 | net_mac mac; |
| 153 | net_arp_packet req; |
| 154 | const uint8_t *pl = 0; |
| 155 | size_t pn = 0; |
| 156 | ck_net_kind k = ck_net_classify(p, rxbuf, len, &mac, &req, &pl, &pn, csum); |
| 157 | if (k == CK_NET_ARP_ASK) { |
| 158 | size_t w = ck_net_arp_reply(p, &req, frame, sizeof frame); |
| 159 | if (w && vnet_tx(&n->dev, frame, w) == VNET_OK) |
| 160 | ck_printf("net: arp answered who-has %u.%u.%u.%u\n", p->ip.b[0], p->ip.b[1], p->ip.b[2], p->ip.b[3]); |
| 161 | continue; |
| 162 | } |
| 163 | if (k != (ck_net_kind)want) continue; |
| 164 | if (k == CK_NET_ARP_GW) p->gw_mac = mac; |
| 165 | if (k == CK_NET_UDP_REPLY) { |
| 166 | /* Keep the reply: move the payload to the front of rxbuf. */ |
| 167 | memmove(rxbuf, pl, pn); |
| 168 | *len_out = pn; |
| 169 | } |
| 170 | return (int)k; |
| 171 | } while (ck_time_us() - t0 < us); |
| 172 | return CK_NET_IGNORED; |
| 173 | } |
| 174 | |
| 175 | static int roundtrip(ck_vnet *n, ck_net_peer *pp) |
| 176 | { |
| 177 | ck_net_peer p; |
| 178 | memset(&p, 0, sizeof p); |
| 179 | memset(pp, 0, sizeof *pp); |
| 180 | memcpy(p.mac.b, n->dev.mac, 6); |
| 181 | static const net_ipv4 me = {{10, 0, 2, 15}}, gw = {{10, 0, 2, 2}}; |
| 182 | p.ip = me; |
| 183 | p.gw_ip = gw; |
| 184 | p.lport = CK_NET_LOCAL_PORT; |
| 185 | p.rport = CK_NET_ECHO_PORT; |
| 186 | unsigned frames = 0, tries = 0; |
| 187 | size_t dummy = 0; |
| 188 | int csum = 0, k = CK_NET_IGNORED; |
| 189 | |
| 190 | for (tries = 1; tries <= ARP_TRIES && k != CK_NET_ARP_GW; tries++) { |
| 191 | size_t w = ck_net_arp_request(&p, frame, sizeof frame); |
| 192 | if (!w || vnet_tx(&n->dev, frame, w) != VNET_OK) return CK_NET_E_TX; |
| 193 | k = wait_for(n, &p, CK_NET_ARP_GW, ARP_WAIT_US, &dummy, &csum, &frames); |
| 194 | if (k < 0) return CK_NET_E_RX; |
| 195 | } |
| 196 | if (k != CK_NET_ARP_GW) { |
| 197 | ck_printf("net: arp who-has 10.0.2.2 FAIL (no reply after %u tries, frames_seen=%u)\n", ARP_TRIES, frames); |
| 198 | return CK_NET_E_ARP; |
| 199 | } |
| 200 | ck_printf("net: arp who-has 10.0.2.2 tell 10.0.2.15 -> %02x:%02x:%02x:%02x:%02x:%02x tries=%u\n", p.gw_mac.b[0], |
| 201 | p.gw_mac.b[1], p.gw_mac.b[2], p.gw_mac.b[3], p.gw_mac.b[4], p.gw_mac.b[5], tries - 1); |
| 202 | |
| 203 | static char msg[] = "AIENOS-CK-NET ping nonce=00000000"; |
| 204 | hex8((uint32_t)ck_time_us() ^ 0x5eedc0deu, msg + sizeof msg - 9); |
| 205 | const size_t mlen = sizeof msg - 1; |
| 206 | unsigned reclaimed = 0; |
| 207 | k = CK_NET_IGNORED; |
| 208 | for (tries = 1; tries <= UDP_TRIES && k != CK_NET_UDP_REPLY; tries++) { |
| 209 | size_t w = ck_net_udp_frame(&p, (uint16_t)(0x4e00u + tries), (const uint8_t *)msg, mlen, frame, sizeof frame); |
| 210 | vnet_err e = w ? vnet_tx(&n->dev, frame, w) : VNET_E_ARG; |
| 211 | if (e != VNET_OK) { |
| 212 | ck_printf("net: udp tx FAIL (%s)\n", vnet_err_name(e)); |
| 213 | return CK_NET_E_TX; |
| 214 | } |
| 215 | /* The device must hand the TX descriptor back (bounded). */ |
| 216 | uint64_t t0 = ck_time_us(); |
| 217 | unsigned r = 0; |
| 218 | while (ck_time_us() - t0 < UDP_WAIT_US) { |
| 219 | r = 0; |
| 220 | if (vnet_tx_reclaim(&n->dev, &r) != VNET_OK) return CK_NET_E_TX; |
| 221 | reclaimed += r; |
| 222 | if (n->dev.q[VNET_TX_QUEUE].in_flight == 0) break; |
| 223 | } |
| 224 | ck_printf("net: udp tx 10.0.2.15:%u -> 10.0.2.2:%u frame=%u bytes payload=\"%s\" try=%u completed=%s\n", |
| 225 | CK_NET_LOCAL_PORT, CK_NET_ECHO_PORT, (unsigned)w, msg, tries, |
| 226 | n->dev.q[VNET_TX_QUEUE].in_flight == 0 ? "yes" : "NO"); |
| 227 | size_t pn = 0; |
| 228 | k = wait_for(n, &p, CK_NET_UDP_REPLY, UDP_WAIT_US, &pn, &csum, &frames); |
| 229 | if (k < 0) return CK_NET_E_RX; |
| 230 | if (k != CK_NET_UDP_REPLY) continue; |
| 231 | static const char pong[] = "AIENOS-CK-NET pong token="; |
| 232 | char shown[160]; |
| 233 | ck_net_printable(rxbuf, pn, shown, sizeof shown); |
| 234 | int mine = has_prefix(rxbuf, pn, pong, sizeof pong - 1) && contains(rxbuf, pn, msg, mlen); |
| 235 | ck_printf("net: udp rx 10.0.2.2:%u -> 10.0.2.15:%u payload_len=%u udp_csum=%s parsed=m6a echo_of_ours=%s " |
| 236 | "payload=\"%s\"\n", |
| 237 | CK_NET_ECHO_PORT, CK_NET_LOCAL_PORT, (unsigned)pn, csum ? "verified" : "absent", mine ? "yes" : "NO", |
| 238 | shown); |
| 239 | if (!mine) k = CK_NET_IGNORED; /* not the reply to our datagram: the next try sends again */ |
| 240 | } |
| 241 | if (k != CK_NET_UDP_REPLY) { |
| 242 | ck_printf("net: udp round trip FAIL (no reply after %u tries, frames_seen=%u)\n", UDP_TRIES, frames); |
| 243 | return CK_NET_E_RX; |
| 244 | } |
| 245 | ck_printf("net: udp round trip ok tx_reclaimed=%u rx_frames=%u\n", reclaimed, frames); |
| 246 | *pp = p; |
| 247 | return CK_NET_OK; |
| 248 | } |
| 249 | |
| 250 | /* SMMU negative test (net_bind.h). After a good round trip, every RX |
| 251 | * descriptor the device still holds is pointed at a pattern-filled page |
| 252 | * that is NOT in the stream's window (and mapped by no other entry of this |
| 253 | * stream's stage-1 table), then one datagram is sent (TX buffer inside the |
| 254 | * window) so the helper's reply makes the device write an RX frame. Pass: |
| 255 | * the SMMU event queue holds an F_TRANSLATION record for this stream at |
| 256 | * that page, the page keeps its pattern, and the positive round trip before |
| 257 | * it raised no fault at all. How the device model reacts is reported, not |
| 258 | * judged: QEMU 8.2 either refuses the descriptor (virtio "bogus descriptor", |
| 259 | * NEEDS_RESET) or maps it through its internal bounce buffer and completes |
| 260 | * the RX, the write-back of which the SMMU refuses as well (rx_completed, |
| 261 | * device_status). recovery() below resets and re-inits the device either way. */ |
| 262 | static int smmu_negative_test(ck_vnet *n, const ck_net_peer *p, uint64_t win_lo, uint64_t win_len) |
| 263 | { |
| 264 | static volatile uint8_t *canary; /* ck_dma_alloc is never freed: once */ |
| 265 | static uint64_t cphys; |
| 266 | if (!canary) canary = ck_dma_alloc(4096, 4096, &cphys); |
| 267 | if (!canary) { |
| 268 | ck_printf("net_smmu_negative: FAIL (no canary page)\n"); |
| 269 | return -1; |
| 270 | } |
| 271 | if (!(cphys + 4096u <= win_lo || cphys >= win_lo + win_len)) { |
| 272 | ck_printf("net_smmu_negative: FAIL (canary page 0x%llx inside the window)\n", (unsigned long long)cphys); |
| 273 | return -1; |
| 274 | } |
| 275 | for (uint32_t i = 0; i < 4096u; i++) canary[i] = CANARY_BYTE; |
| 276 | struct ck_dma_fault fl = {0, 0, 0, 0}; |
| 277 | int before = ck_dma_faults(n->stream_id, 0); /* faults raised by the positive round trip (want 0) */ |
| 278 | |
| 279 | vnet_queue *q = &n->dev.q[VNET_RX_QUEUE]; |
| 280 | unsigned redirected = 0; |
| 281 | for (uint16_t id = 0; id < q->size; id++) { |
| 282 | if (!q->posted[id]) continue; |
| 283 | /* one 64-bit store: the device may read this descriptor concurrently */ |
| 284 | *(volatile uint64_t *)(void *)(n->dev.mem + q->desc_off + 16u * id) = cphys; |
| 285 | redirected++; |
| 286 | } |
| 287 | ck_mb(); /* descriptors rewritten before the datagram that draws the reply */ |
| 288 | uint16_t used0 = *(volatile uint16_t *)(n->dev.mem + q->used_off + 2); |
| 289 | |
| 290 | static char probe[] = "AIENOS-CK-NET ping smmu-probe nonce=00000000"; |
| 291 | hex8((uint32_t)ck_time_us() ^ 0x0badd0e5u, probe + sizeof probe - 9); |
| 292 | size_t w = ck_net_udp_frame(p, 0x4f00u, (const uint8_t *)probe, sizeof probe - 1, frame, sizeof frame); |
| 293 | vnet_err e = w ? vnet_tx(&n->dev, frame, w) : VNET_E_ARG; |
| 294 | ck_printf("net_smmu_negative: rx descriptors redirected=%u to iova=0x%llx (outside window 0x%llx+0x%llx); " |
| 295 | "probe datagram \"%s\" tx=%s\n", |
| 296 | redirected, (unsigned long long)cphys, (unsigned long long)win_lo, (unsigned long long)win_len, probe, |
| 297 | vnet_err_name(e)); |
| 298 | uint32_t st = 0; |
| 299 | uint64_t t0 = ck_time_us(); |
| 300 | while (e == VNET_OK && ck_time_us() - t0 < PROBE_WAIT_US) { |
| 301 | st = o_read(n, VNET_WIN_COMMON, VNET_CC_STATUS, 1); |
| 302 | if (st & VNET_S_NEEDS_RESET) break; |
| 303 | if (*(volatile uint16_t *)(n->dev.mem + q->used_off + 2) != used0) break; /* RX completed (bounce) */ |
| 304 | ck_udelay(1000); |
| 305 | } |
| 306 | ck_udelay(20000); /* let any further event records land */ |
| 307 | st = o_read(n, VNET_WIN_COMMON, VNET_CC_STATUS, 1); |
| 308 | n->dev.broken = 1; /* descriptors no longer point into the region: never use them again */ |
| 309 | int faults = ck_dma_faults(n->stream_id, &fl); |
| 310 | int intact = 1; |
| 311 | for (uint32_t i = 0; i < 4096u; i++) |
| 312 | if (canary[i] != CANARY_BYTE) intact = 0; |
| 313 | uint16_t rx_done = (uint16_t)(*(volatile uint16_t *)(n->dev.mem + q->used_off + 2) - used0); |
| 314 | int ok = e == VNET_OK && before == 0 && faults >= 1 && fl.type == CK_DMA_FAULT_TRANSLATION && |
| 315 | fl.stream_id == n->stream_id && (fl.addr & ~(uint64_t)4095u) == cphys && intact; |
| 316 | ck_printf("net_smmu_negative: %s rx dma outside window iova=0x%llx faults=%d type=0x%x sid=0x%x addr=0x%llx " |
| 317 | "page=%s rx_completed=%u device_status=0x%02x needs_reset=%s faults_during_roundtrip=%d overflow=%d\n", |
| 318 | ok ? "refused" : "FAIL", (unsigned long long)cphys, faults, fl.type, fl.stream_id, |
| 319 | (unsigned long long)fl.addr, intact ? "intact" : "MODIFIED", rx_done, st, |
| 320 | (st & VNET_S_NEEDS_RESET) ? "yes" : "no", before, fl.overflow); |
| 321 | return ok ? 0 : -1; |
| 322 | } |
| 323 | |
| 324 | /* After the negative test: reset + re-init (vnet_init writes status 0 and |
| 325 | * rebuilds every descriptor inside the window) and a second full round |
| 326 | * trip, still confined, with no new SMMU fault. */ |
| 327 | static int recovery(ck_vnet *n, const vnet_ops *ops, const virtio_pci_caps *caps, uint8_t *mem, uint64_t phys, |
| 328 | size_t need) |
| 329 | { |
| 330 | /* Stop the device before vnet_init clears the rings it may still be |
| 331 | * reading (status 0, wait until it reads back 0). */ |
| 332 | o_write(n, VNET_WIN_COMMON, VNET_CC_STATUS, 1, 0); |
| 333 | uint32_t st = 0xffu; |
| 334 | uint64_t t0 = ck_time_us(); |
| 335 | while ((st = o_read(n, VNET_WIN_COMMON, VNET_CC_STATUS, 1)) != 0 && ck_time_us() - t0 < RESET_WAIT_US) |
| 336 | ck_udelay(10); |
| 337 | /* Events the refused probe raised after the negative test drained the |
| 338 | * queue belong to the probe: drop them (reported) so new_faults below |
| 339 | * counts only the second round trip. */ |
| 340 | int late = ck_dma_faults(n->stream_id, 0); |
| 341 | if (st != 0) { |
| 342 | ck_printf("net_smmu_recovery: FAIL (device reset timeout status=0x%02x)\n", st); |
| 343 | return -1; |
| 344 | } |
| 345 | vnet_err e = |
| 346 | vnet_init_flags(&n->dev, ops, caps, mem, phys, need, CK_NET_QSIZE, VNET_INIT_REQUIRE_ACCESS_PLATFORM); |
| 347 | ck_printf("net_smmu_recovery: device reset + reinit %s (%s) access_platform=%s late_probe_faults=%d\n", |
| 348 | e == VNET_OK ? "ok" : "FAIL", vnet_err_name(e), |
| 349 | (n->dev.features >> VNET_F_ACCESS_PLATFORM & 1) ? "yes" : "no", late); |
| 350 | if (e != VNET_OK) return -1; |
| 351 | ck_net_peer p; |
| 352 | int rc = roundtrip(n, &p); |
| 353 | int faults = ck_dma_faults(n->stream_id, 0); |
| 354 | int ok = rc == CK_NET_OK && faults == 0; |
| 355 | ck_printf("net_smmu_recovery: round trip after the fault %s (rc=%d new_faults=%d)\n", ok ? "ok" : "FAIL", rc, |
| 356 | faults); |
| 357 | return ok ? 0 : -1; |
| 358 | } |
| 359 | |
| 360 | int ck_net_bind_selftest(const pci_func *f, const virtio_pci_caps *caps) |
| 361 | { |
| 362 | ck_vnet *n = &g_net; |
| 363 | /* Never forget a device that may still do DMA: a live earlier bind |
| 364 | * (bus master on or stream confined) must be released first. */ |
| 365 | if (ck_net_live()) { |
| 366 | ck_printf("virtio_net: not bound (previous bind still live; release it first)\n"); |
| 367 | return CK_NET_E_ARG; |
| 368 | } |
| 369 | memset(n, 0, sizeof *n); |
| 370 | if (!f || !caps) return CK_NET_E_ARG; |
| 371 | n->pf = f; |
| 372 | size_t need = vnet_mem_size(CK_NET_QSIZE); |
| 373 | uint64_t win = ((uint64_t)need + 4095u) & ~(uint64_t)4095u; |
| 374 | if (need && !g_dma_mem) g_dma_mem = ck_dma_alloc((size_t)win, 4096, &g_dma_phys); |
| 375 | uint8_t *mem = need ? g_dma_mem : 0; |
| 376 | uint64_t phys = g_dma_phys; |
| 377 | if (!mem) { |
| 378 | ck_printf("virtio_net: unavailable (DMA allocation of %u bytes failed)\n", (unsigned)win); |
| 379 | return CK_NET_E_DMA; |
| 380 | } |
| 381 | uint32_t rid = ((uint32_t)f->bus << 8) | ((uint32_t)f->dev << 3) | (uint32_t)f->fn; |
| 382 | struct ck_dma_confinement cf; |
| 383 | int src = ck_dma_confine(f->segment, rid, phys, win, &cf); |
| 384 | if (src != 0) { |
| 385 | ck_printf("dma_gate: virtio_net denied (%s rc=%d), bus master stays off\n", |
| 386 | src == CK_SMMU_ABSENT ? "NoSmmu" : "SmmuNotReady", src); |
| 387 | ck_printf("virtio_net: not bound (SMMU DMA isolation not active)\n"); |
| 388 | return CK_NET_E_DENIED; |
| 389 | } |
| 390 | n->confined = 1; |
| 391 | n->stream_id = cf.stream_id; |
| 392 | ck_printf("smmu_dma_window: virtio_net only iova=0x%llx len=0x%llx rid=0x%x stream_id=0x%x\n", |
| 393 | (unsigned long long)cf.iova, (unsigned long long)cf.len, rid, cf.stream_id); |
| 394 | const virtio_pci_region *r[WIN_N] = {&caps->common_cfg, &caps->notify_cfg, &caps->isr_cfg, &caps->device_cfg}; |
| 395 | for (int i = 0; i < WIN_N; i++) { |
| 396 | n->win[i] = map_window(n, r[i]); |
| 397 | if (!n->win[i] && i != VNET_WIN_ISR) { /* ISR is unused (polled driver) */ |
| 398 | ck_printf("virtio_net: not bound (capability window %d outside its BAR)\n", i); |
| 399 | ck_net_release(); |
| 400 | if (n->confined) return CK_NET_E_SMMU; /* abort refused: stream still confined */ |
| 401 | return CK_NET_E_ARG; |
| 402 | } |
| 403 | } |
| 404 | pci_enable(f, 1); |
| 405 | n->bm_on = 1; |
| 406 | ck_printf("dma_gate: virtio_net granted (Confined), bus master on\n"); |
| 407 | vnet_ops ops = {n, o_read, o_write, o_notify}; |
| 408 | /* Fail closed: without VIRTIO_F_ACCESS_PLATFORM QEMU (and a real device |
| 409 | * behind a bypassing transport) would DMA around the SMMU window. */ |
| 410 | vnet_err e = |
| 411 | vnet_init_flags(&n->dev, &ops, caps, mem, phys, need, CK_NET_QSIZE, VNET_INIT_REQUIRE_ACCESS_PLATFORM); |
| 412 | if (e != VNET_OK) { |
| 413 | ck_printf("virtio_net: init FAIL (%s)\n", vnet_err_name(e)); |
| 414 | ck_net_release(); |
| 415 | if (n->confined) return CK_NET_E_SMMU; /* abort refused: stream still confined */ |
| 416 | return CK_NET_E_INIT; |
| 417 | } |
| 418 | const uint8_t *m = n->dev.mac; |
| 419 | ck_printf("virtio_net: attached %02x:%02x.%u rid=0x%x qsize=rx%u/tx%u mac=%02x:%02x:%02x:%02x:%02x:%02x " |
| 420 | "features=0x%llx access_platform=%s\n", |
| 421 | f->bus, f->dev, f->fn, rid, n->dev.q[VNET_RX_QUEUE].size, n->dev.q[VNET_TX_QUEUE].size, m[0], m[1], m[2], |
| 422 | m[3], m[4], m[5], (unsigned long long)n->dev.features, |
| 423 | (n->dev.features >> VNET_F_ACCESS_PLATFORM & 1) ? "yes" : "no"); |
| 424 | ck_net_peer peer; |
| 425 | memset(&peer, 0, sizeof peer); |
| 426 | int rc = n->dev.has_mac ? roundtrip(n, &peer) : CK_NET_E_INIT; |
| 427 | /* Prove the fence: device DMA outside the window is refused by the SMMU, |
| 428 | * then the device recovers inside it (net_bind.h). */ |
| 429 | if (rc == CK_NET_OK && |
| 430 | (smmu_negative_test(n, &peer, phys, win) != 0 || recovery(n, &ops, caps, mem, phys, need) != 0)) |
| 431 | rc = CK_NET_E_SMMU; |
| 432 | if (!n->dev.has_mac) ck_printf("net: no MAC offered by the device; round trip not attempted\n"); |
| 433 | ck_printf("net: selftest %s (rc=%d)\n", rc == CK_NET_OK ? "PASS" : "FAIL", rc); |
| 434 | ck_net_release(); |
| 435 | /* A refused abort leaves the stream confined. A passing selftest whose |
| 436 | * bus master is still on is not released either. */ |
| 437 | if (n->confined || (rc == CK_NET_OK && ck_net_live())) rc = CK_NET_E_SMMU; |
| 438 | return rc; |
| 439 | } |