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