]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/consport.c
Replace global list for grouplist with list(s) for each exportlist element.
[FreeBSD/FreeBSD.git] / usr.sbin / bhyve / consport.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 NetApp, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/types.h>
35 #ifndef WITHOUT_CAPSICUM
36 #include <sys/capsicum.h>
37 #endif
38 #include <sys/select.h>
39
40 #ifndef WITHOUT_CAPSICUM
41 #include <capsicum_helpers.h>
42 #endif
43 #include <err.h>
44 #include <errno.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <termios.h>
48 #include <unistd.h>
49 #include <stdbool.h>
50 #include <sysexits.h>
51
52 #include "inout.h"
53 #include "pci_lpc.h"
54
55 #define BVM_CONSOLE_PORT        0x220
56 #define BVM_CONS_SIG            ('b' << 8 | 'v')
57
58 static struct termios tio_orig, tio_new;
59
60 static void
61 ttyclose(void)
62 {
63         tcsetattr(STDIN_FILENO, TCSANOW, &tio_orig);
64 }
65
66 static void
67 ttyopen(void)
68 {
69         tcgetattr(STDIN_FILENO, &tio_orig);
70
71         cfmakeraw(&tio_new);
72         tcsetattr(STDIN_FILENO, TCSANOW, &tio_new);     
73
74         atexit(ttyclose);
75 }
76
77 static bool
78 tty_char_available(void)
79 {
80         fd_set rfds;
81         struct timeval tv;
82
83         FD_ZERO(&rfds);
84         FD_SET(STDIN_FILENO, &rfds);
85         tv.tv_sec = 0;
86         tv.tv_usec = 0;
87         if (select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tv) > 0) {
88                 return (true);
89         } else {
90                 return (false);
91         }
92 }
93
94 static int
95 ttyread(void)
96 {
97         char rb;
98
99         if (tty_char_available()) {
100                 read(STDIN_FILENO, &rb, 1);
101                 return (rb & 0xff);
102         } else {
103                 return (-1);
104         }
105 }
106
107 static void
108 ttywrite(unsigned char wb)
109 {
110         (void) write(STDOUT_FILENO, &wb, 1);
111 }
112
113 static int
114 console_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
115                 uint32_t *eax, void *arg)
116 {
117         static int opened;
118 #ifndef WITHOUT_CAPSICUM
119         cap_rights_t rights;
120         cap_ioctl_t cmds[] = { TIOCGETA, TIOCSETA, TIOCGWINSZ };
121 #endif
122
123         if (bytes == 2 && in) {
124                 *eax = BVM_CONS_SIG;
125                 return (0);
126         }
127
128         /*
129          * Guests might probe this port to look for old ISA devices
130          * using single-byte reads.  Return 0xff for those.
131          */
132         if (bytes == 1 && in) {
133                 *eax = 0xff;
134                 return (0);
135         }
136
137         if (bytes != 4)
138                 return (-1);
139
140         if (!opened) {
141 #ifndef WITHOUT_CAPSICUM
142                 cap_rights_init(&rights, CAP_EVENT, CAP_IOCTL, CAP_READ,
143                     CAP_WRITE);
144                 if (caph_rights_limit(STDIN_FILENO, &rights) == -1)
145                         errx(EX_OSERR, "Unable to apply rights for sandbox");
146                 if (caph_ioctls_limit(STDIN_FILENO, cmds, nitems(cmds)) == -1)
147                         errx(EX_OSERR, "Unable to apply rights for sandbox");
148 #endif
149                 ttyopen();
150                 opened = 1;
151         }
152         
153         if (in)
154                 *eax = ttyread();
155         else
156                 ttywrite(*eax);
157
158         return (0);
159 }
160
161 SYSRES_IO(BVM_CONSOLE_PORT, 4);
162
163 static struct inout_port consport = {
164         "bvmcons",
165         BVM_CONSOLE_PORT,
166         1,
167         IOPORT_F_INOUT,
168         console_handler
169 };
170
171 void
172 init_bvmcons(void)
173 {
174
175         register_inout(&consport);
176 }