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