1221828Sgrehan/*-
2221828Sgrehan * Copyright (c) 2011 NetApp, Inc.
3221828Sgrehan * All rights reserved.
4221828Sgrehan *
5221828Sgrehan * Redistribution and use in source and binary forms, with or without
6221828Sgrehan * modification, are permitted provided that the following conditions
7221828Sgrehan * are met:
8221828Sgrehan * 1. Redistributions of source code must retain the above copyright
9221828Sgrehan *    notice, this list of conditions and the following disclaimer.
10221828Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
11221828Sgrehan *    notice, this list of conditions and the following disclaimer in the
12221828Sgrehan *    documentation and/or other materials provided with the distribution.
13221828Sgrehan *
14221828Sgrehan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15221828Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16221828Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17221828Sgrehan * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18221828Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19221828Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20221828Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21221828Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22221828Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23221828Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24221828Sgrehan * SUCH DAMAGE.
25221828Sgrehan *
26221828Sgrehan * $FreeBSD: stable/10/usr.sbin/bhyve/dbgport.c 309401 2016-12-02 08:21:25Z julian $
27221828Sgrehan */
28221828Sgrehan
29221828Sgrehan#include <sys/cdefs.h>
30221828Sgrehan__FBSDID("$FreeBSD: stable/10/usr.sbin/bhyve/dbgport.c 309401 2016-12-02 08:21:25Z julian $");
31221828Sgrehan
32221828Sgrehan#include <sys/types.h>
33221828Sgrehan#include <sys/socket.h>
34221828Sgrehan#include <netinet/in.h>
35309401Sjulian#include <netinet/tcp.h>
36221828Sgrehan#include <sys/uio.h>
37221828Sgrehan
38221828Sgrehan#include <stdio.h>
39221828Sgrehan#include <stdlib.h>
40221828Sgrehan#include <fcntl.h>
41221828Sgrehan#include <unistd.h>
42221828Sgrehan#include <errno.h>
43221828Sgrehan
44221828Sgrehan#include "inout.h"
45221942Sjhb#include "dbgport.h"
46262227Sjhb#include "pci_lpc.h"
47221828Sgrehan
48221828Sgrehan#define	BVM_DBG_PORT	0x224
49242195Sneel#define	BVM_DBG_SIG	('B' << 8 | 'V')
50221828Sgrehan
51221828Sgrehanstatic int listen_fd, conn_fd;
52221828Sgrehan
53221828Sgrehanstatic struct sockaddr_in sin;
54221828Sgrehan
55221828Sgrehanstatic int
56221828Sgrehandbg_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
57221828Sgrehan	    uint32_t *eax, void *arg)
58221828Sgrehan{
59309401Sjulian	int nwritten, nread, printonce;
60309401Sjulian	int on = 1;
61221828Sgrehan	char ch;
62221828Sgrehan
63242195Sneel	if (bytes == 2 && in) {
64242195Sneel		*eax = BVM_DBG_SIG;
65242195Sneel		return (0);
66242195Sneel	}
67242195Sneel
68221828Sgrehan	if (bytes != 4)
69221828Sgrehan		return (-1);
70221828Sgrehan
71221828Sgrehanagain:
72221828Sgrehan	printonce = 0;
73221828Sgrehan	while (conn_fd < 0) {
74221828Sgrehan		if (!printonce) {
75221828Sgrehan			printf("Waiting for connection from gdb\r\n");
76221828Sgrehan			printonce = 1;
77221828Sgrehan		}
78309399Sjulian		conn_fd = accept4(listen_fd, NULL, NULL, SOCK_NONBLOCK);
79309401Sjulian		if (conn_fd >= 0) {
80309401Sjulian			/* Avoid EPIPE after the client drops off. */
81309401Sjulian			(void)setsockopt(conn_fd, SOL_SOCKET, SO_NOSIGPIPE,
82309401Sjulian			    &on, sizeof(on));
83309401Sjulian			/* Improve latency for one byte at a time tranfers. */
84309401Sjulian			(void)setsockopt(conn_fd, IPPROTO_TCP, TCP_NODELAY,
85309401Sjulian			    &on, sizeof(on));
86309401Sjulian		} else if (errno != EINTR) {
87221828Sgrehan			perror("accept");
88309401Sjulian		}
89221828Sgrehan	}
90221828Sgrehan
91221828Sgrehan	if (in) {
92221828Sgrehan		nread = read(conn_fd, &ch, 1);
93221828Sgrehan		if (nread == -1 && errno == EAGAIN)
94221828Sgrehan			*eax = -1;
95221828Sgrehan		else if (nread == 1)
96221828Sgrehan			*eax = ch;
97221828Sgrehan		else {
98221828Sgrehan			close(conn_fd);
99221828Sgrehan			conn_fd = -1;
100221828Sgrehan			goto again;
101221828Sgrehan		}
102221828Sgrehan	} else {
103221828Sgrehan		ch = *eax;
104221828Sgrehan		nwritten = write(conn_fd, &ch, 1);
105221828Sgrehan		if (nwritten != 1) {
106221828Sgrehan			close(conn_fd);
107221828Sgrehan			conn_fd = -1;
108221828Sgrehan			goto again;
109221828Sgrehan		}
110221828Sgrehan	}
111221828Sgrehan	return (0);
112221828Sgrehan}
113221828Sgrehan
114242195Sneelstatic struct inout_port dbgport = {
115242195Sneel	"bvmdbg",
116242195Sneel	BVM_DBG_PORT,
117249321Sneel	1,
118242195Sneel	IOPORT_F_INOUT,
119242195Sneel	dbg_handler
120242195Sneel};
121242195Sneel
122262227SjhbSYSRES_IO(BVM_DBG_PORT, 4);
123262227Sjhb
124242195Sneelvoid
125242195Sneelinit_dbgport(int sport)
126242195Sneel{
127295124Sgrehan	int reuse;
128295124Sgrehan
129242195Sneel	conn_fd = -1;
130242195Sneel
131242195Sneel	if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
132242195Sneel		perror("socket");
133242195Sneel		exit(1);
134242195Sneel	}
135242195Sneel
136242195Sneel	sin.sin_len = sizeof(sin);
137242195Sneel	sin.sin_family = AF_INET;
138242195Sneel	sin.sin_addr.s_addr = htonl(INADDR_ANY);
139242195Sneel	sin.sin_port = htons(sport);
140242195Sneel
141295124Sgrehan	reuse = 1;
142295124Sgrehan	if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse,
143295124Sgrehan	    sizeof(reuse)) < 0) {
144295124Sgrehan		perror("setsockopt");
145295124Sgrehan		exit(1);
146295124Sgrehan	}
147295124Sgrehan
148242195Sneel	if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
149242195Sneel		perror("bind");
150242195Sneel		exit(1);
151242195Sneel	}
152242195Sneel
153242195Sneel	if (listen(listen_fd, 1) < 0) {
154242195Sneel		perror("listen");
155242195Sneel		exit(1);
156242195Sneel	}
157242195Sneel
158242195Sneel	register_inout(&dbgport);
159242195Sneel}
160