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: releng/11.0/usr.sbin/bhyve/dbgport.c 284630 2015-06-20 07:49:08Z jmg $ 27221828Sgrehan */ 28221828Sgrehan 29221828Sgrehan#include <sys/cdefs.h> 30221828Sgrehan__FBSDID("$FreeBSD: releng/11.0/usr.sbin/bhyve/dbgport.c 284630 2015-06-20 07:49:08Z jmg $"); 31221828Sgrehan 32221828Sgrehan#include <sys/types.h> 33221828Sgrehan#include <sys/socket.h> 34221828Sgrehan#include <netinet/in.h> 35221828Sgrehan#include <sys/uio.h> 36221828Sgrehan 37221828Sgrehan#include <stdio.h> 38221828Sgrehan#include <stdlib.h> 39221828Sgrehan#include <fcntl.h> 40221828Sgrehan#include <unistd.h> 41221828Sgrehan#include <errno.h> 42221828Sgrehan 43221828Sgrehan#include "inout.h" 44221942Sjhb#include "dbgport.h" 45261607Sjhb#include "pci_lpc.h" 46221828Sgrehan 47221828Sgrehan#define BVM_DBG_PORT 0x224 48242195Sneel#define BVM_DBG_SIG ('B' << 8 | 'V') 49221828Sgrehan 50221828Sgrehanstatic int listen_fd, conn_fd; 51221828Sgrehan 52221828Sgrehanstatic struct sockaddr_in sin; 53221828Sgrehan 54221828Sgrehanstatic int 55221828Sgrehandbg_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes, 56221828Sgrehan uint32_t *eax, void *arg) 57221828Sgrehan{ 58221828Sgrehan char ch; 59221828Sgrehan int nwritten, nread, printonce; 60221828Sgrehan 61242195Sneel if (bytes == 2 && in) { 62242195Sneel *eax = BVM_DBG_SIG; 63242195Sneel return (0); 64242195Sneel } 65242195Sneel 66221828Sgrehan if (bytes != 4) 67221828Sgrehan return (-1); 68221828Sgrehan 69221828Sgrehanagain: 70221828Sgrehan printonce = 0; 71221828Sgrehan while (conn_fd < 0) { 72221828Sgrehan if (!printonce) { 73221828Sgrehan printf("Waiting for connection from gdb\r\n"); 74221828Sgrehan printonce = 1; 75221828Sgrehan } 76221828Sgrehan conn_fd = accept(listen_fd, NULL, NULL); 77221828Sgrehan if (conn_fd >= 0) 78221828Sgrehan fcntl(conn_fd, F_SETFL, O_NONBLOCK); 79221828Sgrehan else if (errno != EINTR) 80221828Sgrehan perror("accept"); 81221828Sgrehan } 82221828Sgrehan 83221828Sgrehan if (in) { 84221828Sgrehan nread = read(conn_fd, &ch, 1); 85221828Sgrehan if (nread == -1 && errno == EAGAIN) 86221828Sgrehan *eax = -1; 87221828Sgrehan else if (nread == 1) 88221828Sgrehan *eax = ch; 89221828Sgrehan else { 90221828Sgrehan close(conn_fd); 91221828Sgrehan conn_fd = -1; 92221828Sgrehan goto again; 93221828Sgrehan } 94221828Sgrehan } else { 95221828Sgrehan ch = *eax; 96221828Sgrehan nwritten = write(conn_fd, &ch, 1); 97221828Sgrehan if (nwritten != 1) { 98221828Sgrehan close(conn_fd); 99221828Sgrehan conn_fd = -1; 100221828Sgrehan goto again; 101221828Sgrehan } 102221828Sgrehan } 103221828Sgrehan return (0); 104221828Sgrehan} 105221828Sgrehan 106242195Sneelstatic struct inout_port dbgport = { 107242195Sneel "bvmdbg", 108242195Sneel BVM_DBG_PORT, 109249321Sneel 1, 110242195Sneel IOPORT_F_INOUT, 111242195Sneel dbg_handler 112242195Sneel}; 113242195Sneel 114261607SjhbSYSRES_IO(BVM_DBG_PORT, 4); 115261607Sjhb 116242195Sneelvoid 117242195Sneelinit_dbgport(int sport) 118242195Sneel{ 119284630Sjmg int reuse; 120284630Sjmg 121242195Sneel conn_fd = -1; 122242195Sneel 123242195Sneel if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 124242195Sneel perror("socket"); 125242195Sneel exit(1); 126242195Sneel } 127242195Sneel 128242195Sneel sin.sin_len = sizeof(sin); 129242195Sneel sin.sin_family = AF_INET; 130242195Sneel sin.sin_addr.s_addr = htonl(INADDR_ANY); 131242195Sneel sin.sin_port = htons(sport); 132242195Sneel 133284630Sjmg reuse = 1; 134284630Sjmg if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, 135284630Sjmg sizeof(reuse)) < 0) { 136284630Sjmg perror("setsockopt"); 137284630Sjmg exit(1); 138284630Sjmg } 139284630Sjmg 140242195Sneel if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 141242195Sneel perror("bind"); 142242195Sneel exit(1); 143242195Sneel } 144242195Sneel 145242195Sneel if (listen(listen_fd, 1) < 0) { 146242195Sneel perror("listen"); 147242195Sneel exit(1); 148242195Sneel } 149242195Sneel 150242195Sneel register_inout(&dbgport); 151242195Sneel} 152