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