1 /*- 2 * Copyright (c) 2013 Chris Torek <torek @ torek net> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/uio.h> 32 33 #include <stdio.h> 34 #include <stdint.h> 35 #include <pthread.h> 36 37 #include "bhyverun.h" 38 #include "pci_emul.h" 39 #include "virtio.h" 40 41 /* 42 * Functions for dealing with generalized "virtual devices" as 43 * defined by <https://www.google.com/#output=search&q=virtio+spec> 44 */ 45 46 /* 47 * In case we decide to relax the "virtio softc comes at the 48 * front of virtio-based device softc" constraint, let's use 49 * this to convert. 50 */ 51 #define DEV_SOFTC(vs) ((void *)(vs)) 52 53 /* 54 * Link a virtio_softc to its constants, the device softc, and 55 * the PCI emulation. 56 */ 57 void 58 vi_softc_linkup(struct virtio_softc *vs, struct virtio_consts *vc, 59 void *dev_softc, struct pci_devinst *pi, 60 struct vqueue_info *queues) 61 { 62 int i; 63 64 /* vs and dev_softc addresses must match */ 65 assert((void *)vs == dev_softc); 66 vs->vs_vc = vc; 67 vs->vs_pi = pi; 68 pi->pi_arg = vs; 69 70 vs->vs_queues = queues; 71 for (i = 0; i < vc->vc_nvq; i++) { 72 queues[i].vq_vs = vs; 73 queues[i].vq_num = i; 74 } 75 } 76 77 /* 78 * Reset device (device-wide). This erases all queues, i.e., 79 * all the queues become invalid (though we don't wipe out the 80 * internal pointers, we just clear the VQ_ALLOC flag). 81 * 82 * It resets negotiated features to "none". 83 * 84 * If MSI-X is enabled, this also resets all the vectors to NO_VECTOR. 85 */ 86 void 87 vi_reset_dev(struct virtio_softc *vs) 88 { 89 struct vqueue_info *vq; 90 int i, nvq; 91 92 nvq = vs->vs_vc->vc_nvq; 93 for (vq = vs->vs_queues, i = 0; i < nvq; vq++, i++) { 94 vq->vq_flags = 0; 95 vq->vq_last_avail = 0; 96 vq->vq_pfn = 0; 97 vq->vq_msix_idx = VIRTIO_MSI_NO_VECTOR; 98 } 99 vs->vs_negotiated_caps = 0; 100 vs->vs_curq = 0; 101 /* vs->vs_status = 0; -- redundant */ 102 VS_LOCK(vs); 103 if (vs->vs_isr) 104 pci_lintr_deassert(vs->vs_pi); 105 vs->vs_isr = 0; 106 VS_UNLOCK(vs); 107 vs->vs_msix_cfg_idx = VIRTIO_MSI_NO_VECTOR; 108 } 109 110 /* 111 * Set I/O BAR (usually 0) to map PCI config registers. 112 */ 113 void 114 vi_set_io_bar(struct virtio_softc *vs, int barnum) 115 { 116 size_t size; 117 118 /* 119 * ??? should we use CFG0 if MSI-X is disabled? 120 * Existing code did not... 121 */ 122 size = VTCFG_R_CFG1 + vs->vs_vc->vc_cfgsize; 123 pci_emul_alloc_bar(vs->vs_pi, barnum, PCIBAR_IO, size); 124 } 125 126 /* 127 * Initialize MSI-X vector capabilities if we're to use MSI-X, 128 * or MSI capabilities if not. 129 * 130 * We assume we want one MSI-X vector per queue, here, plus one 131 * for the config vec. 132 */ 133 int 134 vi_intr_init(struct virtio_softc *vs, int barnum, int use_msix) 135 { 136 int nvec; 137 138 if (use_msix) { 139 vs->vs_flags |= VIRTIO_USE_MSIX; 140 vi_reset_dev(vs); /* set all vectors to NO_VECTOR */ 141 nvec = vs->vs_vc->vc_nvq + 1; 142 if (pci_emul_add_msixcap(vs->vs_pi, nvec, barnum)) 143 return (1); 144 } else 145 vs->vs_flags &= ~VIRTIO_USE_MSIX; 146 /* Only 1 MSI vector for bhyve */ 147 pci_emul_add_msicap(vs->vs_pi, 1); 148 return (0); 149 } 150 151 /* 152 * Initialize the currently-selected virtio queue (vs->vs_curq). 153 * The guest just gave us a page frame number, from which we can 154 * calculate the addresses of the queue. 155 */ 156 void 157 vi_vq_init(struct virtio_softc *vs, uint32_t pfn) 158 { 159 struct vqueue_info *vq; 160 uint64_t phys; 161 size_t size; 162 char *base; 163 164 vq = &vs->vs_queues[vs->vs_curq]; 165 vq->vq_pfn = pfn; 166 phys = (uint64_t)pfn << VRING_PFN; 167 size = vring_size(vq->vq_qsize); 168 base = paddr_guest2host(vs->vs_pi->pi_vmctx, phys, size); 169 170 /* First page(s) are descriptors... */ 171 vq->vq_desc = (struct virtio_desc *)base; 172 base += vq->vq_qsize * sizeof(struct virtio_desc); 173 174 /* ... immediately followed by "avail" ring (entirely uint16_t's) */ 175 vq->vq_avail = (struct vring_avail *)base; 176 base += (2 + vq->vq_qsize + 1) * sizeof(uint16_t); 177 178 /* Then it's rounded up to the next page... */ 179 base = (char *)roundup2((uintptr_t)base, VRING_ALIGN); 180 181 /* ... and the last page(s) are the used ring. */ 182 vq->vq_used = (struct vring_used *)base; 183 184 /* Mark queue as allocated, and start at 0 when we use it. */ 185 vq->vq_flags = VQ_ALLOC; 186 vq->vq_last_avail = 0; 187 } 188 189 /* 190 * Helper inline for vq_getchain(): record the i'th "real" 191 * descriptor. 192 */ 193 static inline void 194 _vq_record(int i, volatile struct virtio_desc *vd, struct vmctx *ctx, 195 struct iovec *iov, int n_iov, uint16_t *flags) { 196 197 if (i >= n_iov) 198 return; 199 iov[i].iov_base = paddr_guest2host(ctx, vd->vd_addr, vd->vd_len); 200 iov[i].iov_len = vd->vd_len; 201 if (flags != NULL) 202 flags[i] = vd->vd_flags; 203 } 204 #define VQ_MAX_DESCRIPTORS 512 /* see below */ 205 206 /* 207 * Examine the chain of descriptors starting at the "next one" to 208 * make sure that they describe a sensible request. If so, return 209 * the number of "real" descriptors that would be needed/used in 210 * acting on this request. This may be smaller than the number of 211 * available descriptors, e.g., if there are two available but 212 * they are two separate requests, this just returns 1. Or, it 213 * may be larger: if there are indirect descriptors involved, 214 * there may only be one descriptor available but it may be an 215 * indirect pointing to eight more. We return 8 in this case, 216 * i.e., we do not count the indirect descriptors, only the "real" 217 * ones. 218 * 219 * Basically, this vets the vd_flags and vd_next field of each 220 * descriptor and tells you how many are involved. Since some may 221 * be indirect, this also needs the vmctx (in the pci_devinst 222 * at vs->vs_pi) so that it can find indirect descriptors. 223 * 224 * As we process each descriptor, we copy and adjust it (guest to 225 * host address wise, also using the vmtctx) into the given iov[] 226 * array (of the given size). If the array overflows, we stop 227 * placing values into the array but keep processing descriptors, 228 * up to VQ_MAX_DESCRIPTORS, before giving up and returning -1. 229 * So you, the caller, must not assume that iov[] is as big as the 230 * return value (you can process the same thing twice to allocate 231 * a larger iov array if needed, or supply a zero length to find 232 * out how much space is needed). 233 * 234 * If you want to verify the WRITE flag on each descriptor, pass a 235 * non-NULL "flags" pointer to an array of "uint16_t" of the same size 236 * as n_iov and we'll copy each vd_flags field after unwinding any 237 * indirects. 238 * 239 * If some descriptor(s) are invalid, this prints a diagnostic message 240 * and returns -1. If no descriptors are ready now it simply returns 0. 241 * 242 * You are assumed to have done a vq_ring_ready() if needed (note 243 * that vq_has_descs() does one). 244 */ 245 int 246 vq_getchain(struct vqueue_info *vq, 247 struct iovec *iov, int n_iov, uint16_t *flags) 248 { 249 int i; 250 u_int ndesc, n_indir; 251 u_int idx, head, next; 252 volatile struct virtio_desc *vdir, *vindir, *vp; 253 struct vmctx *ctx; 254 struct virtio_softc *vs; 255 const char *name; 256 257 vs = vq->vq_vs; 258 name = vs->vs_vc->vc_name; 259 260 /* 261 * Note: it's the responsibility of the guest not to 262 * update vq->vq_avail->va_idx until all of the descriptors 263 * the guest has written are valid (including all their 264 * vd_next fields and vd_flags). 265 * 266 * Compute (last_avail - va_idx) in integers mod 2**16. This is 267 * the number of descriptors the device has made available 268 * since the last time we updated vq->vq_last_avail. 269 * 270 * We just need to do the subtraction as an unsigned int, 271 * then trim off excess bits. 272 */ 273 idx = vq->vq_last_avail; 274 ndesc = (uint16_t)((u_int)vq->vq_avail->va_idx - idx); 275 if (ndesc == 0) 276 return (0); 277 if (ndesc > vq->vq_qsize) { 278 /* XXX need better way to diagnose issues */ 279 fprintf(stderr, 280 "%s: ndesc (%u) out of range, driver confused?\r\n", 281 name, (u_int)ndesc); 282 return (-1); 283 } 284 285 /* 286 * Now count/parse "involved" descriptors starting from 287 * the head of the chain. 288 * 289 * To prevent loops, we could be more complicated and 290 * check whether we're re-visiting a previously visited 291 * index, but we just abort if the count gets excessive. 292 */ 293 ctx = vs->vs_pi->pi_vmctx; 294 head = vq->vq_avail->va_ring[idx & (vq->vq_qsize - 1)]; 295 next = head; 296 for (i = 0; i < VQ_MAX_DESCRIPTORS; next = vdir->vd_next) { 297 if (next >= vq->vq_qsize) { 298 fprintf(stderr, 299 "%s: descriptor index %u out of range, " 300 "driver confused?\r\n", 301 name, next); 302 return (-1); 303 } 304 vdir = &vq->vq_desc[next]; 305 if ((vdir->vd_flags & VRING_DESC_F_INDIRECT) == 0) { 306 _vq_record(i, vdir, ctx, iov, n_iov, flags); 307 i++; 308 } else if ((vs->vs_negotiated_caps & 309 VIRTIO_RING_F_INDIRECT_DESC) == 0) { 310 fprintf(stderr, 311 "%s: descriptor has forbidden INDIRECT flag, " 312 "driver confused?\r\n", 313 name); 314 return (-1); 315 } else { 316 n_indir = vdir->vd_len / 16; 317 if ((vdir->vd_len & 0xf) || n_indir == 0) { 318 fprintf(stderr, 319 "%s: invalid indir len 0x%x, " 320 "driver confused?\r\n", 321 name, (u_int)vdir->vd_len); 322 return (-1); 323 } 324 vindir = paddr_guest2host(ctx, 325 vdir->vd_addr, vdir->vd_len); 326 /* 327 * Indirects start at the 0th, then follow 328 * their own embedded "next"s until those run 329 * out. Each one's indirect flag must be off 330 * (we don't really have to check, could just 331 * ignore errors...). 332 */ 333 next = 0; 334 for (;;) { 335 vp = &vindir[next]; 336 if (vp->vd_flags & VRING_DESC_F_INDIRECT) { 337 fprintf(stderr, 338 "%s: indirect desc has INDIR flag," 339 " driver confused?\r\n", 340 name); 341 return (-1); 342 } 343 _vq_record(i, vp, ctx, iov, n_iov, flags); 344 if (++i > VQ_MAX_DESCRIPTORS) 345 goto loopy; 346 if ((vp->vd_flags & VRING_DESC_F_NEXT) == 0) 347 break; 348 next = vp->vd_next; 349 if (next >= n_indir) { 350 fprintf(stderr, 351 "%s: invalid next %u > %u, " 352 "driver confused?\r\n", 353 name, (u_int)next, n_indir); 354 return (-1); 355 } 356 } 357 } 358 if ((vdir->vd_flags & VRING_DESC_F_NEXT) == 0) 359 return (i); 360 } 361 loopy: 362 fprintf(stderr, 363 "%s: descriptor loop? count > %d - driver confused?\r\n", 364 name, i); 365 return (-1); 366 } 367 368 /* 369 * Return the currently-first request chain to the guest, setting 370 * its I/O length to the provided value. 371 * 372 * (This chain is the one you handled when you called vq_getchain() 373 * and used its positive return value.) 374 */ 375 void 376 vq_relchain(struct vqueue_info *vq, uint32_t iolen) 377 { 378 uint16_t head, uidx, mask; 379 volatile struct vring_used *vuh; 380 volatile struct virtio_used *vue; 381 382 /* 383 * Notes: 384 * - mask is N-1 where N is a power of 2 so computes x % N 385 * - vuh points to the "used" data shared with guest 386 * - vue points to the "used" ring entry we want to update 387 * - head is the same value we compute in vq_iovecs(). 388 * 389 * (I apologize for the two fields named vu_idx; the 390 * virtio spec calls the one that vue points to, "id"...) 391 */ 392 mask = vq->vq_qsize - 1; 393 vuh = vq->vq_used; 394 head = vq->vq_avail->va_ring[vq->vq_last_avail++ & mask]; 395 396 uidx = vuh->vu_idx; 397 vue = &vuh->vu_ring[uidx++ & mask]; 398 vue->vu_idx = head; /* ie, vue->id = head */ 399 vue->vu_tlen = iolen; 400 vuh->vu_idx = uidx; 401 } 402 403 /* 404 * Driver has finished processing "available" chains and calling 405 * vq_relchain on each one. If driver used all the available 406 * chains, used_all should be set. 407 * 408 * If the "used" index moved we may need to inform the guest, i.e., 409 * deliver an interrupt. Even if the used index did NOT move we 410 * may need to deliver an interrupt, if the avail ring is empty and 411 * we are supposed to interrupt on empty. 412 * 413 * Note that used_all_avail is provided by the caller because it's 414 * a snapshot of the ring state when he decided to finish interrupt 415 * processing -- it's possible that descriptors became available after 416 * that point. (It's also typically a constant 1/True as well.) 417 */ 418 void 419 vq_endchains(struct vqueue_info *vq, int used_all_avail) 420 { 421 struct virtio_softc *vs; 422 uint16_t event_idx, new_idx, old_idx; 423 int intr; 424 425 /* 426 * Interrupt generation: if we're using EVENT_IDX, 427 * interrupt if we've crossed the event threshold. 428 * Otherwise interrupt is generated if we added "used" entries, 429 * but suppressed by VRING_AVAIL_F_NO_INTERRUPT. 430 * 431 * In any case, though, if NOTIFY_ON_EMPTY is set and the 432 * entire avail was processed, we need to interrupt always. 433 */ 434 vs = vq->vq_vs; 435 new_idx = vq->vq_used->vu_idx; 436 old_idx = vq->vq_save_used; 437 if (used_all_avail && 438 (vs->vs_negotiated_caps & VIRTIO_F_NOTIFY_ON_EMPTY)) 439 intr = 1; 440 else if (vs->vs_flags & VIRTIO_EVENT_IDX) { 441 event_idx = VQ_USED_EVENT_IDX(vq); 442 /* 443 * This calculation is per docs and the kernel 444 * (see src/sys/dev/virtio/virtio_ring.h). 445 */ 446 intr = (uint16_t)(new_idx - event_idx - 1) < 447 (uint16_t)(new_idx - old_idx); 448 } else { 449 intr = new_idx != old_idx && 450 !(vq->vq_avail->va_flags & VRING_AVAIL_F_NO_INTERRUPT); 451 } 452 if (intr) 453 vq_interrupt(vs, vq); 454 } 455 456 /* Note: these are in sorted order to make for a fast search */ 457 static struct config_reg { 458 uint16_t cr_offset; /* register offset */ 459 uint8_t cr_size; /* size (bytes) */ 460 uint8_t cr_ro; /* true => reg is read only */ 461 const char *cr_name; /* name of reg */ 462 } config_regs[] = { 463 { VTCFG_R_HOSTCAP, 4, 1, "HOSTCAP" }, 464 { VTCFG_R_GUESTCAP, 4, 0, "GUESTCAP" }, 465 { VTCFG_R_PFN, 4, 0, "PFN" }, 466 { VTCFG_R_QNUM, 2, 1, "QNUM" }, 467 { VTCFG_R_QSEL, 2, 0, "QSEL" }, 468 { VTCFG_R_QNOTIFY, 2, 0, "QNOTIFY" }, 469 { VTCFG_R_STATUS, 1, 0, "STATUS" }, 470 { VTCFG_R_ISR, 1, 0, "ISR" }, 471 { VTCFG_R_CFGVEC, 2, 0, "CFGVEC" }, 472 { VTCFG_R_QVEC, 2, 0, "QVEC" }, 473 }; 474 475 static inline struct config_reg * 476 vi_find_cr(int offset) { 477 u_int hi, lo, mid; 478 struct config_reg *cr; 479 480 lo = 0; 481 hi = sizeof(config_regs) / sizeof(*config_regs) - 1; 482 while (hi >= lo) { 483 mid = (hi + lo) >> 1; 484 cr = &config_regs[mid]; 485 if (cr->cr_offset == offset) 486 return (cr); 487 if (cr->cr_offset < offset) 488 lo = mid + 1; 489 else 490 hi = mid - 1; 491 } 492 return (NULL); 493 } 494 495 /* 496 * Handle pci config space reads. 497 * If it's to the MSI-X info, do that. 498 * If it's part of the virtio standard stuff, do that. 499 * Otherwise dispatch to the actual driver. 500 */ 501 uint64_t 502 vi_pci_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, 503 int baridx, uint64_t offset, int size) 504 { 505 struct virtio_softc *vs = pi->pi_arg; 506 struct virtio_consts *vc; 507 struct config_reg *cr; 508 uint64_t virtio_config_size, max; 509 const char *name; 510 uint32_t newoff; 511 uint32_t value; 512 int error; 513 514 if (vs->vs_flags & VIRTIO_USE_MSIX) { 515 if (baridx == pci_msix_table_bar(pi) || 516 baridx == pci_msix_pba_bar(pi)) { 517 return (pci_emul_msix_tread(pi, offset, size)); 518 } 519 } 520 521 /* XXX probably should do something better than just assert() */ 522 assert(baridx == 0); 523 524 if (vs->vs_mtx) 525 pthread_mutex_lock(vs->vs_mtx); 526 527 vc = vs->vs_vc; 528 name = vc->vc_name; 529 value = size == 1 ? 0xff : size == 2 ? 0xffff : 0xffffffff; 530 531 if (size != 1 && size != 2 && size != 4) 532 goto bad; 533 534 if (pci_msix_enabled(pi)) 535 virtio_config_size = VTCFG_R_CFG1; 536 else 537 virtio_config_size = VTCFG_R_CFG0; 538 539 if (offset >= virtio_config_size) { 540 /* 541 * Subtract off the standard size (including MSI-X 542 * registers if enabled) and dispatch to underlying driver. 543 * If that fails, fall into general code. 544 */ 545 newoff = offset - virtio_config_size; 546 max = vc->vc_cfgsize ? vc->vc_cfgsize : 0x100000000; 547 if (newoff + size > max) 548 goto bad; 549 error = (*vc->vc_cfgread)(DEV_SOFTC(vs), newoff, size, &value); 550 if (!error) 551 goto done; 552 } 553 554 bad: 555 cr = vi_find_cr(offset); 556 if (cr == NULL || cr->cr_size != size) { 557 if (cr != NULL) { 558 /* offset must be OK, so size must be bad */ 559 fprintf(stderr, 560 "%s: read from %s: bad size %d\r\n", 561 name, cr->cr_name, size); 562 } else { 563 fprintf(stderr, 564 "%s: read from bad offset/size %jd/%d\r\n", 565 name, (uintmax_t)offset, size); 566 } 567 goto done; 568 } 569 570 switch (offset) { 571 case VTCFG_R_HOSTCAP: 572 value = vc->vc_hv_caps; 573 break; 574 case VTCFG_R_GUESTCAP: 575 value = vs->vs_negotiated_caps; 576 break; 577 case VTCFG_R_PFN: 578 if (vs->vs_curq < vc->vc_nvq) 579 value = vs->vs_queues[vs->vs_curq].vq_pfn; 580 break; 581 case VTCFG_R_QNUM: 582 value = vs->vs_curq < vc->vc_nvq ? 583 vs->vs_queues[vs->vs_curq].vq_qsize : 0; 584 break; 585 case VTCFG_R_QSEL: 586 value = vs->vs_curq; 587 break; 588 case VTCFG_R_QNOTIFY: 589 value = 0; /* XXX */ 590 break; 591 case VTCFG_R_STATUS: 592 value = vs->vs_status; 593 break; 594 case VTCFG_R_ISR: 595 value = vs->vs_isr; 596 vs->vs_isr = 0; /* a read clears this flag */ 597 if (value) 598 pci_lintr_deassert(pi); 599 break; 600 case VTCFG_R_CFGVEC: 601 value = vs->vs_msix_cfg_idx; 602 break; 603 case VTCFG_R_QVEC: 604 value = vs->vs_curq < vc->vc_nvq ? 605 vs->vs_queues[vs->vs_curq].vq_msix_idx : 606 VIRTIO_MSI_NO_VECTOR; 607 break; 608 } 609 done: 610 if (vs->vs_mtx) 611 pthread_mutex_unlock(vs->vs_mtx); 612 return (value); 613 } 614 615 /* 616 * Handle pci config space writes. 617 * If it's to the MSI-X info, do that. 618 * If it's part of the virtio standard stuff, do that. 619 * Otherwise dispatch to the actual driver. 620 */ 621 void 622 vi_pci_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, 623 int baridx, uint64_t offset, int size, uint64_t value) 624 { 625 struct virtio_softc *vs = pi->pi_arg; 626 struct vqueue_info *vq; 627 struct virtio_consts *vc; 628 struct config_reg *cr; 629 uint64_t virtio_config_size, max; 630 const char *name; 631 uint32_t newoff; 632 int error; 633 634 if (vs->vs_flags & VIRTIO_USE_MSIX) { 635 if (baridx == pci_msix_table_bar(pi) || 636 baridx == pci_msix_pba_bar(pi)) { 637 pci_emul_msix_twrite(pi, offset, size, value); 638 return; 639 } 640 } 641 642 /* XXX probably should do something better than just assert() */ 643 assert(baridx == 0); 644 645 if (vs->vs_mtx) 646 pthread_mutex_lock(vs->vs_mtx); 647 648 vc = vs->vs_vc; 649 name = vc->vc_name; 650 651 if (size != 1 && size != 2 && size != 4) 652 goto bad; 653 654 if (pci_msix_enabled(pi)) 655 virtio_config_size = VTCFG_R_CFG1; 656 else 657 virtio_config_size = VTCFG_R_CFG0; 658 659 if (offset >= virtio_config_size) { 660 /* 661 * Subtract off the standard size (including MSI-X 662 * registers if enabled) and dispatch to underlying driver. 663 */ 664 newoff = offset - virtio_config_size; 665 max = vc->vc_cfgsize ? vc->vc_cfgsize : 0x100000000; 666 if (newoff + size > max) 667 goto bad; 668 error = (*vc->vc_cfgwrite)(DEV_SOFTC(vs), newoff, size, value); 669 if (!error) 670 goto done; 671 } 672 673 bad: 674 cr = vi_find_cr(offset); 675 if (cr == NULL || cr->cr_size != size || cr->cr_ro) { 676 if (cr != NULL) { 677 /* offset must be OK, wrong size and/or reg is R/O */ 678 if (cr->cr_size != size) 679 fprintf(stderr, 680 "%s: write to %s: bad size %d\r\n", 681 name, cr->cr_name, size); 682 if (cr->cr_ro) 683 fprintf(stderr, 684 "%s: write to read-only reg %s\r\n", 685 name, cr->cr_name); 686 } else { 687 fprintf(stderr, 688 "%s: write to bad offset/size %jd/%d\r\n", 689 name, (uintmax_t)offset, size); 690 } 691 goto done; 692 } 693 694 switch (offset) { 695 case VTCFG_R_GUESTCAP: 696 vs->vs_negotiated_caps = value & vc->vc_hv_caps; 697 break; 698 case VTCFG_R_PFN: 699 if (vs->vs_curq >= vc->vc_nvq) 700 goto bad_qindex; 701 vi_vq_init(vs, value); 702 break; 703 case VTCFG_R_QSEL: 704 /* 705 * Note that the guest is allowed to select an 706 * invalid queue; we just need to return a QNUM 707 * of 0 while the bad queue is selected. 708 */ 709 vs->vs_curq = value; 710 break; 711 case VTCFG_R_QNOTIFY: 712 if (value >= vc->vc_nvq) { 713 fprintf(stderr, "%s: queue %d notify out of range\r\n", 714 name, (int)value); 715 goto done; 716 } 717 vq = &vs->vs_queues[value]; 718 if (vq->vq_notify) 719 (*vq->vq_notify)(DEV_SOFTC(vs), vq); 720 else if (vc->vc_qnotify) 721 (*vc->vc_qnotify)(DEV_SOFTC(vs), vq); 722 else 723 fprintf(stderr, 724 "%s: qnotify queue %d: missing vq/vc notify\r\n", 725 name, (int)value); 726 break; 727 case VTCFG_R_STATUS: 728 vs->vs_status = value; 729 if (value == 0) 730 (*vc->vc_reset)(DEV_SOFTC(vs)); 731 break; 732 case VTCFG_R_CFGVEC: 733 vs->vs_msix_cfg_idx = value; 734 break; 735 case VTCFG_R_QVEC: 736 if (vs->vs_curq >= vc->vc_nvq) 737 goto bad_qindex; 738 vq = &vs->vs_queues[vs->vs_curq]; 739 vq->vq_msix_idx = value; 740 break; 741 } 742 goto done; 743 744 bad_qindex: 745 fprintf(stderr, 746 "%s: write config reg %s: curq %d >= max %d\r\n", 747 name, cr->cr_name, vs->vs_curq, vc->vc_nvq); 748 done: 749 if (vs->vs_mtx) 750 pthread_mutex_unlock(vs->vs_mtx); 751 } 752