dbgport.c revision 309401
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: stable/10/usr.sbin/bhyve/dbgport.c 309401 2016-12-02 08:21:25Z julian $
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: stable/10/usr.sbin/bhyve/dbgport.c 309401 2016-12-02 08:21:25Z julian $");
31
32#include <sys/types.h>
33#include <sys/socket.h>
34#include <netinet/in.h>
35#include <netinet/tcp.h>
36#include <sys/uio.h>
37
38#include <stdio.h>
39#include <stdlib.h>
40#include <fcntl.h>
41#include <unistd.h>
42#include <errno.h>
43
44#include "inout.h"
45#include "dbgport.h"
46#include "pci_lpc.h"
47
48#define	BVM_DBG_PORT	0x224
49#define	BVM_DBG_SIG	('B' << 8 | 'V')
50
51static int listen_fd, conn_fd;
52
53static struct sockaddr_in sin;
54
55static int
56dbg_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
57	    uint32_t *eax, void *arg)
58{
59	int nwritten, nread, printonce;
60	int on = 1;
61	char ch;
62
63	if (bytes == 2 && in) {
64		*eax = BVM_DBG_SIG;
65		return (0);
66	}
67
68	if (bytes != 4)
69		return (-1);
70
71again:
72	printonce = 0;
73	while (conn_fd < 0) {
74		if (!printonce) {
75			printf("Waiting for connection from gdb\r\n");
76			printonce = 1;
77		}
78		conn_fd = accept4(listen_fd, NULL, NULL, SOCK_NONBLOCK);
79		if (conn_fd >= 0) {
80			/* Avoid EPIPE after the client drops off. */
81			(void)setsockopt(conn_fd, SOL_SOCKET, SO_NOSIGPIPE,
82			    &on, sizeof(on));
83			/* Improve latency for one byte at a time tranfers. */
84			(void)setsockopt(conn_fd, IPPROTO_TCP, TCP_NODELAY,
85			    &on, sizeof(on));
86		} else if (errno != EINTR) {
87			perror("accept");
88		}
89	}
90
91	if (in) {
92		nread = read(conn_fd, &ch, 1);
93		if (nread == -1 && errno == EAGAIN)
94			*eax = -1;
95		else if (nread == 1)
96			*eax = ch;
97		else {
98			close(conn_fd);
99			conn_fd = -1;
100			goto again;
101		}
102	} else {
103		ch = *eax;
104		nwritten = write(conn_fd, &ch, 1);
105		if (nwritten != 1) {
106			close(conn_fd);
107			conn_fd = -1;
108			goto again;
109		}
110	}
111	return (0);
112}
113
114static struct inout_port dbgport = {
115	"bvmdbg",
116	BVM_DBG_PORT,
117	1,
118	IOPORT_F_INOUT,
119	dbg_handler
120};
121
122SYSRES_IO(BVM_DBG_PORT, 4);
123
124void
125init_dbgport(int sport)
126{
127	int reuse;
128
129	conn_fd = -1;
130
131	if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
132		perror("socket");
133		exit(1);
134	}
135
136	sin.sin_len = sizeof(sin);
137	sin.sin_family = AF_INET;
138	sin.sin_addr.s_addr = htonl(INADDR_ANY);
139	sin.sin_port = htons(sport);
140
141	reuse = 1;
142	if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse,
143	    sizeof(reuse)) < 0) {
144		perror("setsockopt");
145		exit(1);
146	}
147
148	if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
149		perror("bind");
150		exit(1);
151	}
152
153	if (listen(listen_fd, 1) < 0) {
154		perror("listen");
155		exit(1);
156	}
157
158	register_inout(&dbgport);
159}
160