1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2016 iXsystems Inc. 5 * All rights reserved. 6 * 7 * This software was developed by Jakub Klama <jceel@FreeBSD.org> 8 * under sponsorship from iXsystems Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer 15 * in this position and unchanged. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <sys/param.h> 37 #ifndef WITHOUT_CAPSICUM 38 #include <sys/capsicum.h> 39 #endif 40 #include <sys/linker_set.h> 41 #include <sys/uio.h> 42 #include <sys/types.h> 43 #include <sys/socket.h> 44 #include <sys/un.h> 45 46 #include <err.h> 47 #include <errno.h> 48 #include <fcntl.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <stdbool.h> 52 #include <string.h> 53 #include <unistd.h> 54 #include <assert.h> 55 #include <pthread.h> 56 #include <libgen.h> 57 #include <sysexits.h> 58 59 #include "bhyverun.h" 60 #include "pci_emul.h" 61 #include "virtio.h" 62 #include "mevent.h" 63 #include "sockstream.h" 64 65 #define VTCON_RINGSZ 64 66 #define VTCON_MAXPORTS 16 67 #define VTCON_MAXQ (VTCON_MAXPORTS * 2 + 2) 68 69 #define VTCON_DEVICE_READY 0 70 #define VTCON_DEVICE_ADD 1 71 #define VTCON_DEVICE_REMOVE 2 72 #define VTCON_PORT_READY 3 73 #define VTCON_CONSOLE_PORT 4 74 #define VTCON_CONSOLE_RESIZE 5 75 #define VTCON_PORT_OPEN 6 76 #define VTCON_PORT_NAME 7 77 78 #define VTCON_F_SIZE 0 79 #define VTCON_F_MULTIPORT 1 80 #define VTCON_F_EMERG_WRITE 2 81 #define VTCON_S_HOSTCAPS \ 82 (VTCON_F_SIZE | VTCON_F_MULTIPORT | VTCON_F_EMERG_WRITE) 83 84 static int pci_vtcon_debug; 85 #define DPRINTF(params) if (pci_vtcon_debug) printf params 86 #define WPRINTF(params) printf params 87 88 struct pci_vtcon_softc; 89 struct pci_vtcon_port; 90 struct pci_vtcon_config; 91 typedef void (pci_vtcon_cb_t)(struct pci_vtcon_port *, void *, struct iovec *, 92 int); 93 94 struct pci_vtcon_port { 95 struct pci_vtcon_softc * vsp_sc; 96 int vsp_id; 97 const char * vsp_name; 98 bool vsp_enabled; 99 bool vsp_console; 100 bool vsp_rx_ready; 101 bool vsp_open; 102 int vsp_rxq; 103 int vsp_txq; 104 void * vsp_arg; 105 pci_vtcon_cb_t * vsp_cb; 106 }; 107 108 struct pci_vtcon_sock 109 { 110 struct pci_vtcon_port * vss_port; 111 const char * vss_path; 112 struct mevent * vss_server_evp; 113 struct mevent * vss_conn_evp; 114 int vss_server_fd; 115 int vss_conn_fd; 116 bool vss_open; 117 }; 118 119 struct pci_vtcon_softc { 120 struct virtio_softc vsc_vs; 121 struct vqueue_info vsc_queues[VTCON_MAXQ]; 122 pthread_mutex_t vsc_mtx; 123 uint64_t vsc_cfg; 124 uint64_t vsc_features; 125 char * vsc_rootdir; 126 int vsc_kq; 127 int vsc_nports; 128 bool vsc_ready; 129 struct pci_vtcon_port vsc_control_port; 130 struct pci_vtcon_port vsc_ports[VTCON_MAXPORTS]; 131 struct pci_vtcon_config *vsc_config; 132 }; 133 134 struct pci_vtcon_config { 135 uint16_t cols; 136 uint16_t rows; 137 uint32_t max_nr_ports; 138 uint32_t emerg_wr; 139 } __attribute__((packed)); 140 141 struct pci_vtcon_control { 142 uint32_t id; 143 uint16_t event; 144 uint16_t value; 145 } __attribute__((packed)); 146 147 struct pci_vtcon_console_resize { 148 uint16_t cols; 149 uint16_t rows; 150 } __attribute__((packed)); 151 152 static void pci_vtcon_reset(void *); 153 static void pci_vtcon_notify_rx(void *, struct vqueue_info *); 154 static void pci_vtcon_notify_tx(void *, struct vqueue_info *); 155 static int pci_vtcon_cfgread(void *, int, int, uint32_t *); 156 static int pci_vtcon_cfgwrite(void *, int, int, uint32_t); 157 static void pci_vtcon_neg_features(void *, uint64_t); 158 static void pci_vtcon_sock_accept(int, enum ev_type, void *); 159 static void pci_vtcon_sock_rx(int, enum ev_type, void *); 160 static void pci_vtcon_sock_tx(struct pci_vtcon_port *, void *, struct iovec *, 161 int); 162 static void pci_vtcon_control_send(struct pci_vtcon_softc *, 163 struct pci_vtcon_control *, const void *, size_t); 164 static void pci_vtcon_announce_port(struct pci_vtcon_port *); 165 static void pci_vtcon_open_port(struct pci_vtcon_port *, bool); 166 167 static struct virtio_consts vtcon_vi_consts = { 168 "vtcon", /* our name */ 169 VTCON_MAXQ, /* we support VTCON_MAXQ virtqueues */ 170 sizeof(struct pci_vtcon_config), /* config reg size */ 171 pci_vtcon_reset, /* reset */ 172 NULL, /* device-wide qnotify */ 173 pci_vtcon_cfgread, /* read virtio config */ 174 pci_vtcon_cfgwrite, /* write virtio config */ 175 pci_vtcon_neg_features, /* apply negotiated features */ 176 VTCON_S_HOSTCAPS, /* our capabilities */ 177 }; 178 179 180 static void 181 pci_vtcon_reset(void *vsc) 182 { 183 struct pci_vtcon_softc *sc; 184 185 sc = vsc; 186 187 DPRINTF(("vtcon: device reset requested!\n")); 188 vi_reset_dev(&sc->vsc_vs); 189 } 190 191 static void 192 pci_vtcon_neg_features(void *vsc, uint64_t negotiated_features) 193 { 194 struct pci_vtcon_softc *sc = vsc; 195 196 sc->vsc_features = negotiated_features; 197 } 198 199 static int 200 pci_vtcon_cfgread(void *vsc, int offset, int size, uint32_t *retval) 201 { 202 struct pci_vtcon_softc *sc = vsc; 203 void *ptr; 204 205 ptr = (uint8_t *)sc->vsc_config + offset; 206 memcpy(retval, ptr, size); 207 return (0); 208 } 209 210 static int 211 pci_vtcon_cfgwrite(void *vsc, int offset, int size, uint32_t val) 212 { 213 214 return (0); 215 } 216 217 static inline struct pci_vtcon_port * 218 pci_vtcon_vq_to_port(struct pci_vtcon_softc *sc, struct vqueue_info *vq) 219 { 220 uint16_t num = vq->vq_num; 221 222 if (num == 0 || num == 1) 223 return (&sc->vsc_ports[0]); 224 225 if (num == 2 || num == 3) 226 return (&sc->vsc_control_port); 227 228 return (&sc->vsc_ports[(num / 2) - 1]); 229 } 230 231 static inline struct vqueue_info * 232 pci_vtcon_port_to_vq(struct pci_vtcon_port *port, bool tx_queue) 233 { 234 int qnum; 235 236 qnum = tx_queue ? port->vsp_txq : port->vsp_rxq; 237 return (&port->vsp_sc->vsc_queues[qnum]); 238 } 239 240 static struct pci_vtcon_port * 241 pci_vtcon_port_add(struct pci_vtcon_softc *sc, const char *name, 242 pci_vtcon_cb_t *cb, void *arg) 243 { 244 struct pci_vtcon_port *port; 245 246 if (sc->vsc_nports == VTCON_MAXPORTS) { 247 errno = EBUSY; 248 return (NULL); 249 } 250 251 port = &sc->vsc_ports[sc->vsc_nports++]; 252 port->vsp_id = sc->vsc_nports - 1; 253 port->vsp_sc = sc; 254 port->vsp_name = name; 255 port->vsp_cb = cb; 256 port->vsp_arg = arg; 257 258 if (port->vsp_id == 0) { 259 /* port0 */ 260 port->vsp_txq = 0; 261 port->vsp_rxq = 1; 262 } else { 263 port->vsp_txq = sc->vsc_nports * 2; 264 port->vsp_rxq = port->vsp_txq + 1; 265 } 266 267 port->vsp_enabled = true; 268 return (port); 269 } 270 271 static int 272 pci_vtcon_sock_add(struct pci_vtcon_softc *sc, const char *name, 273 const char *path) 274 { 275 struct pci_vtcon_sock *sock; 276 struct sockaddr_un sun; 277 char *pathcopy; 278 int s = -1, fd = -1, error = 0; 279 #ifndef WITHOUT_CAPSICUM 280 cap_rights_t rights; 281 #endif 282 283 sock = calloc(1, sizeof(struct pci_vtcon_sock)); 284 if (sock == NULL) { 285 error = -1; 286 goto out; 287 } 288 289 s = socket(AF_UNIX, SOCK_STREAM, 0); 290 if (s < 0) { 291 error = -1; 292 goto out; 293 } 294 295 pathcopy = strdup(path); 296 if (pathcopy == NULL) { 297 error = -1; 298 goto out; 299 } 300 301 fd = open(dirname(pathcopy), O_RDONLY | O_DIRECTORY); 302 if (fd < 0) { 303 free(pathcopy); 304 error = -1; 305 goto out; 306 } 307 308 sun.sun_family = AF_UNIX; 309 sun.sun_len = sizeof(struct sockaddr_un); 310 strcpy(pathcopy, path); 311 strlcpy(sun.sun_path, basename(pathcopy), sizeof(sun.sun_path)); 312 free(pathcopy); 313 314 if (bindat(fd, s, (struct sockaddr *)&sun, sun.sun_len) < 0) { 315 error = -1; 316 goto out; 317 } 318 319 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) { 320 error = -1; 321 goto out; 322 } 323 324 if (listen(s, 1) < 0) { 325 error = -1; 326 goto out; 327 } 328 329 #ifndef WITHOUT_CAPSICUM 330 cap_rights_init(&rights, CAP_ACCEPT, CAP_EVENT, CAP_READ, CAP_WRITE); 331 if (cap_rights_limit(s, &rights) == -1 && errno != ENOSYS) 332 errx(EX_OSERR, "Unable to apply rights for sandbox"); 333 #endif 334 335 sock->vss_port = pci_vtcon_port_add(sc, name, pci_vtcon_sock_tx, sock); 336 if (sock->vss_port == NULL) { 337 error = -1; 338 goto out; 339 } 340 341 sock->vss_open = false; 342 sock->vss_conn_fd = -1; 343 sock->vss_server_fd = s; 344 sock->vss_server_evp = mevent_add(s, EVF_READ, pci_vtcon_sock_accept, 345 sock); 346 347 if (sock->vss_server_evp == NULL) { 348 error = -1; 349 goto out; 350 } 351 352 out: 353 if (fd != -1) 354 close(fd); 355 356 if (error != 0 && s != -1) 357 close(s); 358 359 return (error); 360 } 361 362 static void 363 pci_vtcon_sock_accept(int fd __unused, enum ev_type t __unused, void *arg) 364 { 365 struct pci_vtcon_sock *sock = (struct pci_vtcon_sock *)arg; 366 int s; 367 368 s = accept(sock->vss_server_fd, NULL, NULL); 369 if (s < 0) 370 return; 371 372 if (sock->vss_open) { 373 close(s); 374 return; 375 } 376 377 sock->vss_open = true; 378 sock->vss_conn_fd = s; 379 sock->vss_conn_evp = mevent_add(s, EVF_READ, pci_vtcon_sock_rx, sock); 380 381 pci_vtcon_open_port(sock->vss_port, true); 382 } 383 384 static void 385 pci_vtcon_sock_rx(int fd __unused, enum ev_type t __unused, void *arg) 386 { 387 struct pci_vtcon_port *port; 388 struct pci_vtcon_sock *sock = (struct pci_vtcon_sock *)arg; 389 struct vqueue_info *vq; 390 struct iovec iov; 391 static char dummybuf[2048]; 392 int len, n; 393 uint16_t idx; 394 395 port = sock->vss_port; 396 vq = pci_vtcon_port_to_vq(port, true); 397 398 if (!sock->vss_open || !port->vsp_rx_ready) { 399 len = read(sock->vss_conn_fd, dummybuf, sizeof(dummybuf)); 400 if (len == 0) 401 goto close; 402 403 return; 404 } 405 406 if (!vq_has_descs(vq)) { 407 len = read(sock->vss_conn_fd, dummybuf, sizeof(dummybuf)); 408 vq_endchains(vq, 1); 409 if (len == 0) 410 goto close; 411 412 return; 413 } 414 415 do { 416 n = vq_getchain(vq, &idx, &iov, 1, NULL); 417 len = readv(sock->vss_conn_fd, &iov, n); 418 419 if (len == 0 || (len < 0 && errno == EWOULDBLOCK)) { 420 vq_retchain(vq); 421 vq_endchains(vq, 0); 422 if (len == 0) 423 goto close; 424 425 return; 426 } 427 428 vq_relchain(vq, idx, len); 429 } while (vq_has_descs(vq)); 430 431 vq_endchains(vq, 1); 432 433 close: 434 mevent_delete_close(sock->vss_conn_evp); 435 sock->vss_conn_fd = -1; 436 sock->vss_open = false; 437 } 438 439 static void 440 pci_vtcon_sock_tx(struct pci_vtcon_port *port, void *arg, struct iovec *iov, 441 int niov) 442 { 443 struct pci_vtcon_sock *sock; 444 int i, ret; 445 446 sock = (struct pci_vtcon_sock *)arg; 447 448 if (sock->vss_conn_fd == -1) 449 return; 450 451 for (i = 0; i < niov; i++) { 452 ret = stream_write(sock->vss_conn_fd, iov[i].iov_base, 453 iov[i].iov_len); 454 if (ret <= 0) 455 break; 456 } 457 458 if (ret <= 0) { 459 mevent_delete_close(sock->vss_conn_evp); 460 sock->vss_conn_fd = -1; 461 sock->vss_open = false; 462 } 463 } 464 465 static void 466 pci_vtcon_control_tx(struct pci_vtcon_port *port, void *arg, struct iovec *iov, 467 int niov) 468 { 469 struct pci_vtcon_softc *sc; 470 struct pci_vtcon_port *tmp; 471 struct pci_vtcon_control resp, *ctrl; 472 int i; 473 474 assert(niov == 1); 475 476 sc = port->vsp_sc; 477 ctrl = (struct pci_vtcon_control *)iov->iov_base; 478 479 switch (ctrl->event) { 480 case VTCON_DEVICE_READY: 481 sc->vsc_ready = true; 482 /* set port ready events for registered ports */ 483 for (i = 0; i < VTCON_MAXPORTS; i++) { 484 tmp = &sc->vsc_ports[i]; 485 if (tmp->vsp_enabled) 486 pci_vtcon_announce_port(tmp); 487 488 if (tmp->vsp_open) 489 pci_vtcon_open_port(tmp, true); 490 } 491 break; 492 493 case VTCON_PORT_READY: 494 if (ctrl->id >= sc->vsc_nports) { 495 WPRINTF(("VTCON_PORT_READY event for unknown port %d\n", 496 ctrl->id)); 497 return; 498 } 499 500 tmp = &sc->vsc_ports[ctrl->id]; 501 if (tmp->vsp_console) { 502 resp.event = VTCON_CONSOLE_PORT; 503 resp.id = ctrl->id; 504 resp.value = 1; 505 pci_vtcon_control_send(sc, &resp, NULL, 0); 506 } 507 break; 508 } 509 } 510 511 static void 512 pci_vtcon_announce_port(struct pci_vtcon_port *port) 513 { 514 struct pci_vtcon_control event; 515 516 event.id = port->vsp_id; 517 event.event = VTCON_DEVICE_ADD; 518 event.value = 1; 519 pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0); 520 521 event.event = VTCON_PORT_NAME; 522 pci_vtcon_control_send(port->vsp_sc, &event, port->vsp_name, 523 strlen(port->vsp_name)); 524 } 525 526 static void 527 pci_vtcon_open_port(struct pci_vtcon_port *port, bool open) 528 { 529 struct pci_vtcon_control event; 530 531 if (!port->vsp_sc->vsc_ready) { 532 port->vsp_open = true; 533 return; 534 } 535 536 event.id = port->vsp_id; 537 event.event = VTCON_PORT_OPEN; 538 event.value = (int)open; 539 pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0); 540 } 541 542 static void 543 pci_vtcon_control_send(struct pci_vtcon_softc *sc, 544 struct pci_vtcon_control *ctrl, const void *payload, size_t len) 545 { 546 struct vqueue_info *vq; 547 struct iovec iov; 548 uint16_t idx; 549 int n; 550 551 vq = pci_vtcon_port_to_vq(&sc->vsc_control_port, true); 552 553 if (!vq_has_descs(vq)) 554 return; 555 556 n = vq_getchain(vq, &idx, &iov, 1, NULL); 557 558 assert(n == 1); 559 560 memcpy(iov.iov_base, ctrl, sizeof(struct pci_vtcon_control)); 561 if (payload != NULL && len > 0) 562 memcpy(iov.iov_base + sizeof(struct pci_vtcon_control), 563 payload, len); 564 565 vq_relchain(vq, idx, sizeof(struct pci_vtcon_control) + len); 566 vq_endchains(vq, 1); 567 } 568 569 570 static void 571 pci_vtcon_notify_tx(void *vsc, struct vqueue_info *vq) 572 { 573 struct pci_vtcon_softc *sc; 574 struct pci_vtcon_port *port; 575 struct iovec iov[1]; 576 uint16_t idx, n; 577 uint16_t flags[8]; 578 579 sc = vsc; 580 port = pci_vtcon_vq_to_port(sc, vq); 581 582 while (vq_has_descs(vq)) { 583 n = vq_getchain(vq, &idx, iov, 1, flags); 584 assert(n >= 1); 585 if (port != NULL) 586 port->vsp_cb(port, port->vsp_arg, iov, 1); 587 588 /* 589 * Release this chain and handle more 590 */ 591 vq_relchain(vq, idx, 0); 592 } 593 vq_endchains(vq, 1); /* Generate interrupt if appropriate. */ 594 } 595 596 static void 597 pci_vtcon_notify_rx(void *vsc, struct vqueue_info *vq) 598 { 599 struct pci_vtcon_softc *sc; 600 struct pci_vtcon_port *port; 601 602 sc = vsc; 603 port = pci_vtcon_vq_to_port(sc, vq); 604 605 if (!port->vsp_rx_ready) { 606 port->vsp_rx_ready = 1; 607 vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY; 608 } 609 } 610 611 static int 612 pci_vtcon_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) 613 { 614 struct pci_vtcon_softc *sc; 615 char *portname = NULL; 616 char *portpath = NULL; 617 char *opt; 618 int i; 619 620 sc = calloc(1, sizeof(struct pci_vtcon_softc)); 621 sc->vsc_config = calloc(1, sizeof(struct pci_vtcon_config)); 622 sc->vsc_config->max_nr_ports = VTCON_MAXPORTS; 623 sc->vsc_config->cols = 80; 624 sc->vsc_config->rows = 25; 625 626 vi_softc_linkup(&sc->vsc_vs, &vtcon_vi_consts, sc, pi, sc->vsc_queues); 627 sc->vsc_vs.vs_mtx = &sc->vsc_mtx; 628 629 for (i = 0; i < VTCON_MAXQ; i++) { 630 sc->vsc_queues[i].vq_qsize = VTCON_RINGSZ; 631 sc->vsc_queues[i].vq_notify = i % 2 == 0 632 ? pci_vtcon_notify_rx 633 : pci_vtcon_notify_tx; 634 } 635 636 /* initialize config space */ 637 pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_CONSOLE); 638 pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR); 639 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_SIMPLECOMM); 640 pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_CONSOLE); 641 pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR); 642 643 if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix())) 644 return (1); 645 vi_set_io_bar(&sc->vsc_vs, 0); 646 647 /* create control port */ 648 sc->vsc_control_port.vsp_sc = sc; 649 sc->vsc_control_port.vsp_txq = 2; 650 sc->vsc_control_port.vsp_rxq = 3; 651 sc->vsc_control_port.vsp_cb = pci_vtcon_control_tx; 652 sc->vsc_control_port.vsp_enabled = true; 653 654 while ((opt = strsep(&opts, ",")) != NULL) { 655 portname = strsep(&opt, "="); 656 portpath = opt; 657 658 /* create port */ 659 if (pci_vtcon_sock_add(sc, portname, portpath) < 0) { 660 fprintf(stderr, "cannot create port %s: %s\n", 661 portname, strerror(errno)); 662 return (1); 663 } 664 } 665 666 return (0); 667 } 668 669 struct pci_devemu pci_de_vcon = { 670 .pe_emu = "virtio-console", 671 .pe_init = pci_vtcon_init, 672 .pe_barwrite = vi_pci_write, 673 .pe_barread = vi_pci_read 674 }; 675 PCI_EMUL_SET(pci_de_vcon); 676