consport.c revision 264277
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: head/usr.sbin/bhyve/consport.c 264277 2014-04-08 21:02:03Z jhb $
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/usr.sbin/bhyve/consport.c 264277 2014-04-08 21:02:03Z jhb $");
31
32#include <sys/types.h>
33#include <sys/select.h>
34
35#include <stdio.h>
36#include <stdlib.h>
37#include <termios.h>
38#include <unistd.h>
39#include <stdbool.h>
40
41#include "inout.h"
42#include "pci_lpc.h"
43
44#define	BVM_CONSOLE_PORT	0x220
45#define	BVM_CONS_SIG		('b' << 8 | 'v')
46
47static struct termios tio_orig, tio_new;
48
49static void
50ttyclose(void)
51{
52	tcsetattr(STDIN_FILENO, TCSANOW, &tio_orig);
53}
54
55static void
56ttyopen(void)
57{
58	tcgetattr(STDIN_FILENO, &tio_orig);
59
60	cfmakeraw(&tio_new);
61	tcsetattr(STDIN_FILENO, TCSANOW, &tio_new);
62
63	atexit(ttyclose);
64}
65
66static bool
67tty_char_available(void)
68{
69        fd_set rfds;
70        struct timeval tv;
71
72        FD_ZERO(&rfds);
73        FD_SET(STDIN_FILENO, &rfds);
74        tv.tv_sec = 0;
75        tv.tv_usec = 0;
76        if (select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tv) > 0) {
77		return (true);
78	} else {
79		return (false);
80	}
81}
82
83static int
84ttyread(void)
85{
86	char rb;
87
88	if (tty_char_available()) {
89		read(STDIN_FILENO, &rb, 1);
90		return (rb & 0xff);
91	} else {
92		return (-1);
93	}
94}
95
96static void
97ttywrite(unsigned char wb)
98{
99	(void) write(STDOUT_FILENO, &wb, 1);
100}
101
102static int
103console_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
104		uint32_t *eax, void *arg)
105{
106	static int opened;
107
108	if (bytes == 2 && in) {
109		*eax = BVM_CONS_SIG;
110		return (0);
111	}
112
113	/*
114	 * Guests might probe this port to look for old ISA devices
115	 * using single-byte reads.  Return 0xff for those.
116	 */
117	if (bytes == 1 && in) {
118		*eax = 0xff;
119		return (0);
120	}
121
122	if (bytes != 4)
123		return (-1);
124
125	if (!opened) {
126		ttyopen();
127		opened = 1;
128	}
129
130	if (in)
131		*eax = ttyread();
132	else
133		ttywrite(*eax);
134
135	return (0);
136}
137
138SYSRES_IO(BVM_CONSOLE_PORT, 4);
139
140static struct inout_port consport = {
141	"bvmcons",
142	BVM_CONSOLE_PORT,
143	1,
144	IOPORT_F_INOUT,
145	console_handler
146};
147
148void
149init_bvmcons(void)
150{
151
152	register_inout(&consport);
153}
154