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
46#define	BVM_DBG_PORT	0x224
47#define	BVM_DBG_SIG	('B' << 8 | 'V')
48
49static int listen_fd, conn_fd;
50
51static struct sockaddr_in sin;
52
53static int
54dbg_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
55	    uint32_t *eax, void *arg)
56{
57	char ch;
58	int nwritten, nread, printonce;
59
60	if (bytes == 2 && in) {
61		*eax = BVM_DBG_SIG;
62		return (0);
63	}
64
65	if (bytes != 4)
66		return (-1);
67
68again:
69	printonce = 0;
70	while (conn_fd < 0) {
71		if (!printonce) {
72			printf("Waiting for connection from gdb\r\n");
73			printonce = 1;
74		}
75		conn_fd = accept(listen_fd, NULL, NULL);
76		if (conn_fd >= 0)
77			fcntl(conn_fd, F_SETFL, O_NONBLOCK);
78		else if (errno != EINTR)
79			perror("accept");
80	}
81
82	if (in) {
83		nread = read(conn_fd, &ch, 1);
84		if (nread == -1 && errno == EAGAIN)
85			*eax = -1;
86		else if (nread == 1)
87			*eax = ch;
88		else {
89			close(conn_fd);
90			conn_fd = -1;
91			goto again;
92		}
93	} else {
94		ch = *eax;
95		nwritten = write(conn_fd, &ch, 1);
96		if (nwritten != 1) {
97			close(conn_fd);
98			conn_fd = -1;
99			goto again;
100		}
101	}
102	return (0);
103}
104
105static struct inout_port dbgport = {
106	"bvmdbg",
107	BVM_DBG_PORT,
108	1,
109	IOPORT_F_INOUT,
110	dbg_handler
111};
112
113void
114init_dbgport(int sport)
115{
116	conn_fd = -1;
117
118	if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
119		perror("socket");
120		exit(1);
121	}
122
123	sin.sin_len = sizeof(sin);
124	sin.sin_family = AF_INET;
125	sin.sin_addr.s_addr = htonl(INADDR_ANY);
126	sin.sin_port = htons(sport);
127
128	if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
129		perror("bind");
130		exit(1);
131	}
132
133	if (listen(listen_fd, 1) < 0) {
134		perror("listen");
135		exit(1);
136	}
137
138	register_inout(&dbgport);
139}
140