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