dbgport.c revision 330449
1/*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2011 NetApp, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD: stable/11/usr.sbin/bhyve/dbgport.c 330449 2018-03-05 07:26:05Z eadler $ 29 */ 30 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD: stable/11/usr.sbin/bhyve/dbgport.c 330449 2018-03-05 07:26:05Z eadler $"); 33 34#include <sys/types.h> 35#ifndef WITHOUT_CAPSICUM 36#include <sys/capsicum.h> 37#endif 38#include <sys/socket.h> 39#include <netinet/in.h> 40#include <netinet/tcp.h> 41#include <sys/uio.h> 42 43#include <err.h> 44#include <stdio.h> 45#include <stdlib.h> 46#include <sysexits.h> 47#include <fcntl.h> 48#include <unistd.h> 49#include <errno.h> 50 51#include "inout.h" 52#include "dbgport.h" 53#include "pci_lpc.h" 54 55#define BVM_DBG_PORT 0x224 56#define BVM_DBG_SIG ('B' << 8 | 'V') 57 58static int listen_fd, conn_fd; 59 60static struct sockaddr_in sin; 61 62static int 63dbg_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, 64 uint32_t *eax, void *arg) 65{ 66 int nwritten, nread, printonce; 67 int on = 1; 68 char ch; 69 70 if (bytes == 2 && in) { 71 *eax = BVM_DBG_SIG; 72 return (0); 73 } 74 75 if (bytes != 4) 76 return (-1); 77 78again: 79 printonce = 0; 80 while (conn_fd < 0) { 81 if (!printonce) { 82 printf("Waiting for connection from gdb\r\n"); 83 printonce = 1; 84 } 85 conn_fd = accept4(listen_fd, NULL, NULL, SOCK_NONBLOCK); 86 if (conn_fd >= 0) { 87 /* Avoid EPIPE after the client drops off. */ 88 (void)setsockopt(conn_fd, SOL_SOCKET, SO_NOSIGPIPE, 89 &on, sizeof(on)); 90 /* Improve latency for one byte at a time tranfers. */ 91 (void)setsockopt(conn_fd, IPPROTO_TCP, TCP_NODELAY, 92 &on, sizeof(on)); 93 } else if (errno != EINTR) { 94 perror("accept"); 95 } 96 } 97 98 if (in) { 99 nread = read(conn_fd, &ch, 1); 100 if (nread == -1 && errno == EAGAIN) 101 *eax = -1; 102 else if (nread == 1) 103 *eax = ch; 104 else { 105 close(conn_fd); 106 conn_fd = -1; 107 goto again; 108 } 109 } else { 110 ch = *eax; 111 nwritten = write(conn_fd, &ch, 1); 112 if (nwritten != 1) { 113 close(conn_fd); 114 conn_fd = -1; 115 goto again; 116 } 117 } 118 return (0); 119} 120 121static struct inout_port dbgport = { 122 "bvmdbg", 123 BVM_DBG_PORT, 124 1, 125 IOPORT_F_INOUT, 126 dbg_handler 127}; 128 129SYSRES_IO(BVM_DBG_PORT, 4); 130 131void 132init_dbgport(int sport) 133{ 134 int reuse; 135#ifndef WITHOUT_CAPSICUM 136 cap_rights_t rights; 137#endif 138 139 conn_fd = -1; 140 141 if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 142 perror("socket"); 143 exit(1); 144 } 145 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); 150 151 reuse = 1; 152 if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, 153 sizeof(reuse)) < 0) { 154 perror("setsockopt"); 155 exit(1); 156 } 157 158 if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 159 perror("bind"); 160 exit(1); 161 } 162 163 if (listen(listen_fd, 1) < 0) { 164 perror("listen"); 165 exit(1); 166 } 167 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"); 172#endif 173 174 register_inout(&dbgport); 175} 176