2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2016 iXsystems Inc.
7 * This software was developed by Jakub Klama <jceel@FreeBSD.org>
8 * under sponsorship from iXsystems Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
33 #include <sys/cdefs.h>
34 #include <sys/param.h>
35 #ifndef WITHOUT_CAPSICUM
36 #include <sys/capsicum.h>
38 #include <sys/linker_set.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
44 #ifndef WITHOUT_CAPSICUM
45 #include <capsicum_helpers.h>
66 #include "sockstream.h"
68 #define VTCON_RINGSZ 64
69 #define VTCON_MAXPORTS 16
70 #define VTCON_MAXQ (VTCON_MAXPORTS * 2 + 2)
72 #define VTCON_DEVICE_READY 0
73 #define VTCON_DEVICE_ADD 1
74 #define VTCON_DEVICE_REMOVE 2
75 #define VTCON_PORT_READY 3
76 #define VTCON_CONSOLE_PORT 4
77 #define VTCON_CONSOLE_RESIZE 5
78 #define VTCON_PORT_OPEN 6
79 #define VTCON_PORT_NAME 7
81 #define VTCON_F_SIZE 0
82 #define VTCON_F_MULTIPORT 1
83 #define VTCON_F_EMERG_WRITE 2
84 #define VTCON_S_HOSTCAPS \
85 (VTCON_F_SIZE | VTCON_F_MULTIPORT | VTCON_F_EMERG_WRITE)
87 static int pci_vtcon_debug;
88 #define DPRINTF(params) if (pci_vtcon_debug) PRINTLN params
89 #define WPRINTF(params) PRINTLN params
91 struct pci_vtcon_softc;
92 struct pci_vtcon_port;
93 struct pci_vtcon_config;
94 typedef void (pci_vtcon_cb_t)(struct pci_vtcon_port *, void *, struct iovec *,
97 struct pci_vtcon_port {
98 struct pci_vtcon_softc * vsp_sc;
100 const char * vsp_name;
108 pci_vtcon_cb_t * vsp_cb;
111 struct pci_vtcon_sock
113 struct pci_vtcon_port * vss_port;
114 const char * vss_path;
115 struct mevent * vss_server_evp;
116 struct mevent * vss_conn_evp;
122 struct pci_vtcon_softc {
123 struct virtio_softc vsc_vs;
124 struct vqueue_info vsc_queues[VTCON_MAXQ];
125 pthread_mutex_t vsc_mtx;
127 uint64_t vsc_features;
131 struct pci_vtcon_port vsc_control_port;
132 struct pci_vtcon_port vsc_ports[VTCON_MAXPORTS];
133 struct pci_vtcon_config *vsc_config;
136 struct pci_vtcon_config {
139 uint32_t max_nr_ports;
141 } __attribute__((packed));
143 struct pci_vtcon_control {
147 } __attribute__((packed));
149 struct pci_vtcon_console_resize {
152 } __attribute__((packed));
154 static void pci_vtcon_reset(void *);
155 static void pci_vtcon_notify_rx(void *, struct vqueue_info *);
156 static void pci_vtcon_notify_tx(void *, struct vqueue_info *);
157 static int pci_vtcon_cfgread(void *, int, int, uint32_t *);
158 static int pci_vtcon_cfgwrite(void *, int, int, uint32_t);
159 static void pci_vtcon_neg_features(void *, uint64_t);
160 static void pci_vtcon_sock_accept(int, enum ev_type, void *);
161 static void pci_vtcon_sock_rx(int, enum ev_type, void *);
162 static void pci_vtcon_sock_tx(struct pci_vtcon_port *, void *, struct iovec *,
164 static void pci_vtcon_control_send(struct pci_vtcon_softc *,
165 struct pci_vtcon_control *, const void *, size_t);
166 static void pci_vtcon_announce_port(struct pci_vtcon_port *);
167 static void pci_vtcon_open_port(struct pci_vtcon_port *, bool);
169 static struct virtio_consts vtcon_vi_consts = {
171 .vc_nvq = VTCON_MAXQ,
172 .vc_cfgsize = sizeof(struct pci_vtcon_config),
173 .vc_reset = pci_vtcon_reset,
174 .vc_cfgread = pci_vtcon_cfgread,
175 .vc_cfgwrite = pci_vtcon_cfgwrite,
176 .vc_apply_features = pci_vtcon_neg_features,
177 .vc_hv_caps = VTCON_S_HOSTCAPS,
181 pci_vtcon_reset(void *vsc)
183 struct pci_vtcon_softc *sc;
187 DPRINTF(("vtcon: device reset requested!"));
188 vi_reset_dev(&sc->vsc_vs);
192 pci_vtcon_neg_features(void *vsc, uint64_t negotiated_features)
194 struct pci_vtcon_softc *sc = vsc;
196 sc->vsc_features = negotiated_features;
200 pci_vtcon_cfgread(void *vsc, int offset, int size, uint32_t *retval)
202 struct pci_vtcon_softc *sc = vsc;
205 ptr = (uint8_t *)sc->vsc_config + offset;
206 memcpy(retval, ptr, size);
211 pci_vtcon_cfgwrite(void *vsc __unused, int offset __unused, int size __unused,
212 uint32_t val __unused)
217 static inline struct pci_vtcon_port *
218 pci_vtcon_vq_to_port(struct pci_vtcon_softc *sc, struct vqueue_info *vq)
220 uint16_t num = vq->vq_num;
222 if (num == 0 || num == 1)
223 return (&sc->vsc_ports[0]);
225 if (num == 2 || num == 3)
226 return (&sc->vsc_control_port);
228 return (&sc->vsc_ports[(num / 2) - 1]);
231 static inline struct vqueue_info *
232 pci_vtcon_port_to_vq(struct pci_vtcon_port *port, bool tx_queue)
236 qnum = tx_queue ? port->vsp_txq : port->vsp_rxq;
237 return (&port->vsp_sc->vsc_queues[qnum]);
240 static struct pci_vtcon_port *
241 pci_vtcon_port_add(struct pci_vtcon_softc *sc, int port_id, const char *name,
242 pci_vtcon_cb_t *cb, void *arg)
244 struct pci_vtcon_port *port;
246 port = &sc->vsc_ports[port_id];
247 if (port->vsp_enabled) {
251 port->vsp_id = port_id;
253 port->vsp_name = name;
257 if (port->vsp_id == 0) {
262 port->vsp_txq = (port_id + 1) * 2;
263 port->vsp_rxq = port->vsp_txq + 1;
266 port->vsp_enabled = true;
271 pci_vtcon_sock_add(struct pci_vtcon_softc *sc, const char *port_name,
274 struct pci_vtcon_sock *sock = NULL;
275 struct sockaddr_un sun;
276 const char *name, *path;
279 int s = -1, fd = -1, error = 0;
280 #ifndef WITHOUT_CAPSICUM
284 port = strtol(port_name, &cp, 0);
285 if (*cp != '\0' || port < 0 || port >= VTCON_MAXPORTS) {
286 EPRINTLN("vtcon: Invalid port %s", port_name);
291 path = get_config_value_node(nvl, "path");
293 EPRINTLN("vtcon: required path missing for port %ld", port);
298 sock = calloc(1, sizeof(struct pci_vtcon_sock));
304 s = socket(AF_UNIX, SOCK_STREAM, 0);
310 pathcopy = strdup(path);
311 if (pathcopy == NULL) {
316 fd = open(dirname(pathcopy), O_RDONLY | O_DIRECTORY);
323 sun.sun_family = AF_UNIX;
324 sun.sun_len = sizeof(struct sockaddr_un);
325 strcpy(pathcopy, path);
326 strlcpy(sun.sun_path, basename(pathcopy), sizeof(sun.sun_path));
329 if (bindat(fd, s, (struct sockaddr *)&sun, sun.sun_len) < 0) {
334 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
339 if (listen(s, 1) < 0) {
344 #ifndef WITHOUT_CAPSICUM
345 cap_rights_init(&rights, CAP_ACCEPT, CAP_EVENT, CAP_READ, CAP_WRITE);
346 if (caph_rights_limit(s, &rights) == -1)
347 errx(EX_OSERR, "Unable to apply rights for sandbox");
350 name = get_config_value_node(nvl, "name");
352 EPRINTLN("vtcon: required name missing for port %ld", port);
356 sock->vss_port = pci_vtcon_port_add(sc, port, name, pci_vtcon_sock_tx, sock);
357 if (sock->vss_port == NULL) {
362 sock->vss_open = false;
363 sock->vss_conn_fd = -1;
364 sock->vss_server_fd = s;
365 sock->vss_server_evp = mevent_add(s, EVF_READ, pci_vtcon_sock_accept,
368 if (sock->vss_server_evp == NULL) {
387 pci_vtcon_sock_accept(int fd __unused, enum ev_type t __unused, void *arg)
389 struct pci_vtcon_sock *sock = (struct pci_vtcon_sock *)arg;
392 s = accept(sock->vss_server_fd, NULL, NULL);
396 if (sock->vss_open) {
401 sock->vss_open = true;
402 sock->vss_conn_fd = s;
403 sock->vss_conn_evp = mevent_add(s, EVF_READ, pci_vtcon_sock_rx, sock);
405 pci_vtcon_open_port(sock->vss_port, true);
409 pci_vtcon_sock_rx(int fd __unused, enum ev_type t __unused, void *arg)
411 struct pci_vtcon_port *port;
412 struct pci_vtcon_sock *sock = (struct pci_vtcon_sock *)arg;
413 struct vqueue_info *vq;
416 static char dummybuf[2048];
419 port = sock->vss_port;
420 vq = pci_vtcon_port_to_vq(port, true);
422 if (!sock->vss_open || !port->vsp_rx_ready) {
423 len = read(sock->vss_conn_fd, dummybuf, sizeof(dummybuf));
430 if (!vq_has_descs(vq)) {
431 len = read(sock->vss_conn_fd, dummybuf, sizeof(dummybuf));
440 n = vq_getchain(vq, &iov, 1, &req);
442 len = readv(sock->vss_conn_fd, &iov, n);
444 if (len == 0 || (len < 0 && errno == EWOULDBLOCK)) {
453 vq_relchain(vq, req.idx, len);
454 } while (vq_has_descs(vq));
459 mevent_delete_close(sock->vss_conn_evp);
460 sock->vss_conn_fd = -1;
461 sock->vss_open = false;
465 pci_vtcon_sock_tx(struct pci_vtcon_port *port __unused, void *arg __unused,
466 struct iovec *iov, int niov)
468 struct pci_vtcon_sock *sock;
471 sock = (struct pci_vtcon_sock *)arg;
473 if (sock->vss_conn_fd == -1)
476 for (i = 0; i < niov; i++) {
477 ret = stream_write(sock->vss_conn_fd, iov[i].iov_base,
484 mevent_delete_close(sock->vss_conn_evp);
485 sock->vss_conn_fd = -1;
486 sock->vss_open = false;
491 pci_vtcon_control_tx(struct pci_vtcon_port *port, void *arg __unused,
492 struct iovec *iov, int niov)
494 struct pci_vtcon_softc *sc;
495 struct pci_vtcon_port *tmp;
496 struct pci_vtcon_control resp, *ctrl;
502 ctrl = (struct pci_vtcon_control *)iov->iov_base;
504 switch (ctrl->event) {
505 case VTCON_DEVICE_READY:
506 sc->vsc_ready = true;
507 /* set port ready events for registered ports */
508 for (i = 0; i < VTCON_MAXPORTS; i++) {
509 tmp = &sc->vsc_ports[i];
510 if (tmp->vsp_enabled)
511 pci_vtcon_announce_port(tmp);
514 pci_vtcon_open_port(tmp, true);
518 case VTCON_PORT_READY:
519 tmp = &sc->vsc_ports[ctrl->id];
520 if (ctrl->id >= VTCON_MAXPORTS || !tmp->vsp_enabled) {
521 WPRINTF(("VTCON_PORT_READY event for unknown port %d",
526 if (tmp->vsp_console) {
527 resp.event = VTCON_CONSOLE_PORT;
530 pci_vtcon_control_send(sc, &resp, NULL, 0);
537 pci_vtcon_announce_port(struct pci_vtcon_port *port)
539 struct pci_vtcon_control event;
541 event.id = port->vsp_id;
542 event.event = VTCON_DEVICE_ADD;
544 pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0);
546 event.event = VTCON_PORT_NAME;
547 pci_vtcon_control_send(port->vsp_sc, &event, port->vsp_name,
548 strlen(port->vsp_name));
552 pci_vtcon_open_port(struct pci_vtcon_port *port, bool open)
554 struct pci_vtcon_control event;
556 if (!port->vsp_sc->vsc_ready) {
557 port->vsp_open = true;
561 event.id = port->vsp_id;
562 event.event = VTCON_PORT_OPEN;
563 event.value = (int)open;
564 pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0);
568 pci_vtcon_control_send(struct pci_vtcon_softc *sc,
569 struct pci_vtcon_control *ctrl, const void *payload, size_t len)
571 struct vqueue_info *vq;
576 vq = pci_vtcon_port_to_vq(&sc->vsc_control_port, true);
578 if (!vq_has_descs(vq))
581 n = vq_getchain(vq, &iov, 1, &req);
584 memcpy(iov.iov_base, ctrl, sizeof(struct pci_vtcon_control));
585 if (payload != NULL && len > 0)
586 memcpy((uint8_t *)iov.iov_base +
587 sizeof(struct pci_vtcon_control), payload, len);
589 vq_relchain(vq, req.idx, sizeof(struct pci_vtcon_control) + len);
595 pci_vtcon_notify_tx(void *vsc, struct vqueue_info *vq)
597 struct pci_vtcon_softc *sc;
598 struct pci_vtcon_port *port;
604 port = pci_vtcon_vq_to_port(sc, vq);
606 while (vq_has_descs(vq)) {
607 n = vq_getchain(vq, iov, 1, &req);
610 port->vsp_cb(port, port->vsp_arg, iov, 1);
613 * Release this chain and handle more
615 vq_relchain(vq, req.idx, 0);
617 vq_endchains(vq, 1); /* Generate interrupt if appropriate. */
621 pci_vtcon_notify_rx(void *vsc, struct vqueue_info *vq)
623 struct pci_vtcon_softc *sc;
624 struct pci_vtcon_port *port;
627 port = pci_vtcon_vq_to_port(sc, vq);
629 if (!port->vsp_rx_ready) {
630 port->vsp_rx_ready = 1;
636 * Each console device has a "port" node which contains nodes for
637 * each port. Ports are numbered starting at 0.
640 pci_vtcon_legacy_config_port(nvlist_t *nvl, int port, char *opt)
643 char node_name[sizeof("XX")];
646 name = strsep(&opt, "=");
649 EPRINTLN("vtcon: port %s requires a path", name);
652 if (port >= VTCON_MAXPORTS) {
653 EPRINTLN("vtcon: too many ports");
656 snprintf(node_name, sizeof(node_name), "%d", port);
657 port_nvl = create_relative_config_node(nvl, node_name);
658 set_config_value_node(port_nvl, "name", name);
659 set_config_value_node(port_nvl, "path", path);
664 pci_vtcon_legacy_config(nvlist_t *nvl, const char *opts)
666 char *opt, *str, *tofree;
670 ports_nvl = create_relative_config_node(nvl, "port");
671 tofree = str = strdup(opts);
674 while ((opt = strsep(&str, ",")) != NULL) {
675 error = pci_vtcon_legacy_config_port(ports_nvl, port, opt);
685 pci_vtcon_init(struct pci_devinst *pi, nvlist_t *nvl)
687 struct pci_vtcon_softc *sc;
691 sc = calloc(1, sizeof(struct pci_vtcon_softc));
692 sc->vsc_config = calloc(1, sizeof(struct pci_vtcon_config));
693 sc->vsc_config->max_nr_ports = VTCON_MAXPORTS;
694 sc->vsc_config->cols = 80;
695 sc->vsc_config->rows = 25;
697 pthread_mutex_init(&sc->vsc_mtx, NULL);
699 vi_softc_linkup(&sc->vsc_vs, &vtcon_vi_consts, sc, pi, sc->vsc_queues);
700 sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
702 for (i = 0; i < VTCON_MAXQ; i++) {
703 sc->vsc_queues[i].vq_qsize = VTCON_RINGSZ;
704 sc->vsc_queues[i].vq_notify = i % 2 == 0
705 ? pci_vtcon_notify_rx
706 : pci_vtcon_notify_tx;
709 /* initialize config space */
710 pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_CONSOLE);
711 pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
712 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_SIMPLECOMM);
713 pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_ID_CONSOLE);
714 pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
716 if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
718 vi_set_io_bar(&sc->vsc_vs, 0);
720 /* create control port */
721 sc->vsc_control_port.vsp_sc = sc;
722 sc->vsc_control_port.vsp_txq = 2;
723 sc->vsc_control_port.vsp_rxq = 3;
724 sc->vsc_control_port.vsp_cb = pci_vtcon_control_tx;
725 sc->vsc_control_port.vsp_enabled = true;
727 ports_nvl = find_relative_config_node(nvl, "port");
728 if (ports_nvl != NULL) {
734 while ((name = nvlist_next(ports_nvl, &type, &cookie)) !=
736 if (type != NV_TYPE_NVLIST)
739 if (pci_vtcon_sock_add(sc, name,
740 nvlist_get_nvlist(ports_nvl, name)) < 0) {
741 EPRINTLN("cannot create port %s: %s",
742 name, strerror(errno));
751 static const struct pci_devemu pci_de_vcon = {
752 .pe_emu = "virtio-console",
753 .pe_init = pci_vtcon_init,
754 .pe_barwrite = vi_pci_write,
755 .pe_barread = vi_pci_read,
756 .pe_legacy_config = pci_vtcon_legacy_config,
758 PCI_EMUL_SET(pci_de_vcon);