xref: /NextBSD/usr.sbin/bhyve/dbgport.c (revision 287e3b14e9552995def1802ec9c5034f4adf28ec)
1 /*-
2  * Copyright (c) 2011 NetApp, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <sys/uio.h>
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <fcntl.h>
40 #include <unistd.h>
41 #include <errno.h>
42 
43 #include "inout.h"
44 #include "dbgport.h"
45 #include "pci_lpc.h"
46 
47 #define	BVM_DBG_PORT	0x224
48 #define	BVM_DBG_SIG	('B' << 8 | 'V')
49 
50 static int listen_fd, conn_fd;
51 
52 static struct sockaddr_in sin;
53 
54 static int
dbg_handler(struct vmctx * ctx,int vcpu,int in,int port,int bytes,uint32_t * eax,void * arg)55 dbg_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
56 	    uint32_t *eax, void *arg)
57 {
58 	char ch;
59 	int nwritten, nread, printonce;
60 
61 	if (bytes == 2 && in) {
62 		*eax = BVM_DBG_SIG;
63 		return (0);
64 	}
65 
66 	if (bytes != 4)
67 		return (-1);
68 
69 again:
70 	printonce = 0;
71 	while (conn_fd < 0) {
72 		if (!printonce) {
73 			printf("Waiting for connection from gdb\r\n");
74 			printonce = 1;
75 		}
76 		conn_fd = accept(listen_fd, NULL, NULL);
77 		if (conn_fd >= 0)
78 			fcntl(conn_fd, F_SETFL, O_NONBLOCK);
79 		else if (errno != EINTR)
80 			perror("accept");
81 	}
82 
83 	if (in) {
84 		nread = read(conn_fd, &ch, 1);
85 		if (nread == -1 && errno == EAGAIN)
86 			*eax = -1;
87 		else if (nread == 1)
88 			*eax = ch;
89 		else {
90 			close(conn_fd);
91 			conn_fd = -1;
92 			goto again;
93 		}
94 	} else {
95 		ch = *eax;
96 		nwritten = write(conn_fd, &ch, 1);
97 		if (nwritten != 1) {
98 			close(conn_fd);
99 			conn_fd = -1;
100 			goto again;
101 		}
102 	}
103 	return (0);
104 }
105 
106 static struct inout_port dbgport = {
107 	"bvmdbg",
108 	BVM_DBG_PORT,
109 	1,
110 	IOPORT_F_INOUT,
111 	dbg_handler
112 };
113 
114 SYSRES_IO(BVM_DBG_PORT, 4);
115 
116 void
init_dbgport(int sport)117 init_dbgport(int sport)
118 {
119 	int reuse;
120 
121 	conn_fd = -1;
122 
123 	if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
124 		perror("socket");
125 		exit(1);
126 	}
127 
128 	sin.sin_len = sizeof(sin);
129 	sin.sin_family = AF_INET;
130 	sin.sin_addr.s_addr = htonl(INADDR_ANY);
131 	sin.sin_port = htons(sport);
132 
133 	reuse = 1;
134 	if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse,
135 	    sizeof(reuse)) < 0) {
136 		perror("setsockopt");
137 		exit(1);
138 	}
139 
140 	if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
141 		perror("bind");
142 		exit(1);
143 	}
144 
145 	if (listen(listen_fd, 1) < 0) {
146 		perror("listen");
147 		exit(1);
148 	}
149 
150 	register_inout(&dbgport);
151 }
152