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/socket.h>
39 #include <netinet/in.h>
40 #include <netinet/tcp.h>
55 #define BVM_DBG_PORT 0x224
56 #define BVM_DBG_SIG ('B' << 8 | 'V')
58 static int listen_fd, conn_fd;
60 static struct sockaddr_in sin;
63 dbg_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
64 uint32_t *eax, void *arg)
66 int nwritten, nread, printonce;
70 if (bytes == 2 && in) {
82 printf("Waiting for connection from gdb\r\n");
85 conn_fd = accept4(listen_fd, NULL, NULL, SOCK_NONBLOCK);
87 /* Avoid EPIPE after the client drops off. */
88 (void)setsockopt(conn_fd, SOL_SOCKET, SO_NOSIGPIPE,
90 /* Improve latency for one byte at a time tranfers. */
91 (void)setsockopt(conn_fd, IPPROTO_TCP, TCP_NODELAY,
93 } else if (errno != EINTR) {
99 nread = read(conn_fd, &ch, 1);
100 if (nread == -1 && errno == EAGAIN)
111 nwritten = write(conn_fd, &ch, 1);
121 static struct inout_port dbgport = {
129 SYSRES_IO(BVM_DBG_PORT, 4);
132 init_dbgport(int sport)
135 #ifndef WITHOUT_CAPSICUM
141 if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
142 perror("cannot create socket");
146 sin.sin_len = sizeof(sin);
147 sin.sin_family = AF_INET;
148 sin.sin_addr.s_addr = htonl(INADDR_ANY);
149 sin.sin_port = htons(sport);
152 if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse,
153 sizeof(reuse)) < 0) {
154 perror("cannot set socket options");
158 if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
159 perror("cannot bind socket");
163 if (listen(listen_fd, 1) < 0) {
164 perror("cannot listen socket");
168 #ifndef WITHOUT_CAPSICUM
169 cap_rights_init(&rights, CAP_ACCEPT, CAP_READ, CAP_WRITE);
170 if (cap_rights_limit(listen_fd, &rights) == -1 && errno != ENOSYS)
171 errx(EX_OSERR, "Unable to apply rights for sandbox");
174 register_inout(&dbgport);