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