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