2 * Copyright (c) 2011 NetApp, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
48 #define BVM_DBG_PORT 0x224
49 #define BVM_DBG_SIG ('B' << 8 | 'V')
51 static int listen_fd, conn_fd;
53 static struct sockaddr_in sin;
56 dbg_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
57 uint32_t *eax, void *arg)
59 int nwritten, nread, printonce;
63 if (bytes == 2 && in) {
75 printf("Waiting for connection from gdb\r\n");
78 conn_fd = accept4(listen_fd, NULL, NULL, SOCK_NONBLOCK);
80 /* Avoid EPIPE after the client drops off. */
81 (void)setsockopt(conn_fd, SOL_SOCKET, SO_NOSIGPIPE,
83 /* Improve latency for one byte at a time tranfers. */
84 (void)setsockopt(conn_fd, IPPROTO_TCP, TCP_NODELAY,
86 } else if (errno != EINTR) {
92 nread = read(conn_fd, &ch, 1);
93 if (nread == -1 && errno == EAGAIN)
104 nwritten = write(conn_fd, &ch, 1);
114 static struct inout_port dbgport = {
122 SYSRES_IO(BVM_DBG_PORT, 4);
125 init_dbgport(int sport)
131 if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
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);
142 if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse,
143 sizeof(reuse)) < 0) {
144 perror("setsockopt");
148 if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
153 if (listen(listen_fd, 1) < 0) {
158 register_inout(&dbgport);