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>
43 #ifndef WITHOUT_CAPSICUM
44 #include <capsicum_helpers.h>
58 #define BVM_DBG_PORT 0x224
59 #define BVM_DBG_SIG ('B' << 8 | 'V')
61 static int listen_fd, conn_fd;
63 static struct sockaddr_in sin;
66 dbg_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
67 uint32_t *eax, void *arg)
69 int nwritten, nread, printonce;
73 if (bytes == 2 && in) {
85 printf("Waiting for connection from gdb\r\n");
88 conn_fd = accept4(listen_fd, NULL, NULL, SOCK_NONBLOCK);
90 /* Avoid EPIPE after the client drops off. */
91 (void)setsockopt(conn_fd, SOL_SOCKET, SO_NOSIGPIPE,
93 /* Improve latency for one byte at a time tranfers. */
94 (void)setsockopt(conn_fd, IPPROTO_TCP, TCP_NODELAY,
96 } else if (errno != EINTR) {
102 nread = read(conn_fd, &ch, 1);
103 if (nread == -1 && errno == EAGAIN)
114 nwritten = write(conn_fd, &ch, 1);
124 static struct inout_port dbgport = {
132 SYSRES_IO(BVM_DBG_PORT, 4);
135 init_dbgport(int sport)
138 #ifndef WITHOUT_CAPSICUM
144 if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
145 perror("cannot create socket");
149 sin.sin_len = sizeof(sin);
150 sin.sin_family = AF_INET;
151 sin.sin_addr.s_addr = htonl(INADDR_ANY);
152 sin.sin_port = htons(sport);
155 if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse,
156 sizeof(reuse)) < 0) {
157 perror("cannot set socket options");
161 if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
162 perror("cannot bind socket");
166 if (listen(listen_fd, 1) < 0) {
167 perror("cannot listen socket");
171 #ifndef WITHOUT_CAPSICUM
172 cap_rights_init(&rights, CAP_ACCEPT, CAP_READ, CAP_WRITE);
173 if (caph_rights_limit(listen_fd, &rights) == -1)
174 errx(EX_OSERR, "Unable to apply rights for sandbox");
177 register_inout(&dbgport);