2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 NetApp, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/types.h>
35 #ifndef WITHOUT_CAPSICUM
36 #include <sys/capsicum.h>
38 #include <sys/select.h>
40 #ifndef WITHOUT_CAPSICUM
41 #include <capsicum_helpers.h>
56 #define BVM_CONSOLE_PORT 0x220
57 #define BVM_CONS_SIG ('b' << 8 | 'v')
59 static struct termios tio_orig, tio_new;
64 tcsetattr(STDIN_FILENO, TCSANOW, &tio_orig);
70 tcgetattr(STDIN_FILENO, &tio_orig);
73 tcsetattr(STDIN_FILENO, TCSANOW, &tio_new);
80 tty_char_available(void)
86 FD_SET(STDIN_FILENO, &rfds);
89 if (select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tv) > 0) {
101 if (tty_char_available()) {
102 read(STDIN_FILENO, &rb, 1);
110 ttywrite(unsigned char wb)
112 (void) write(STDOUT_FILENO, &wb, 1);
116 console_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
117 uint32_t *eax, void *arg)
120 #ifndef WITHOUT_CAPSICUM
122 cap_ioctl_t cmds[] = { TIOCGETA, TIOCSETA, TIOCGWINSZ };
125 if (bytes == 2 && in) {
131 * Guests might probe this port to look for old ISA devices
132 * using single-byte reads. Return 0xff for those.
134 if (bytes == 1 && in) {
143 #ifndef WITHOUT_CAPSICUM
144 cap_rights_init(&rights, CAP_EVENT, CAP_IOCTL, CAP_READ,
146 if (caph_rights_limit(STDIN_FILENO, &rights) == -1)
147 errx(EX_OSERR, "Unable to apply rights for sandbox");
148 if (caph_ioctls_limit(STDIN_FILENO, cmds, nitems(cmds)) == -1)
149 errx(EX_OSERR, "Unable to apply rights for sandbox");
163 SYSRES_IO(BVM_CONSOLE_PORT, 4);
165 static struct inout_port consport = {
177 register_inout(&consport);