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$
27221828Sgrehan */
28221828Sgrehan
29221828Sgrehan#include <sys/cdefs.h>
30221828Sgrehan__FBSDID("$FreeBSD$");
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"
45221828Sgrehan
46221828Sgrehan#define	BVM_DBG_PORT	0x224
47242195Sneel#define	BVM_DBG_SIG	('B' << 8 | 'V')
48221828Sgrehan
49221828Sgrehanstatic int listen_fd, conn_fd;
50221828Sgrehan
51221828Sgrehanstatic struct sockaddr_in sin;
52221828Sgrehan
53221828Sgrehanstatic int
54221828Sgrehandbg_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
55221828Sgrehan	    uint32_t *eax, void *arg)
56221828Sgrehan{
57221828Sgrehan	char ch;
58221828Sgrehan	int nwritten, nread, printonce;
59221828Sgrehan
60242195Sneel	if (bytes == 2 && in) {
61242195Sneel		*eax = BVM_DBG_SIG;
62242195Sneel		return (0);
63242195Sneel	}
64242195Sneel
65221828Sgrehan	if (bytes != 4)
66221828Sgrehan		return (-1);
67221828Sgrehan
68221828Sgrehanagain:
69221828Sgrehan	printonce = 0;
70221828Sgrehan	while (conn_fd < 0) {
71221828Sgrehan		if (!printonce) {
72221828Sgrehan			printf("Waiting for connection from gdb\r\n");
73221828Sgrehan			printonce = 1;
74221828Sgrehan		}
75221828Sgrehan		conn_fd = accept(listen_fd, NULL, NULL);
76221828Sgrehan		if (conn_fd >= 0)
77221828Sgrehan			fcntl(conn_fd, F_SETFL, O_NONBLOCK);
78221828Sgrehan		else if (errno != EINTR)
79221828Sgrehan			perror("accept");
80221828Sgrehan	}
81221828Sgrehan
82221828Sgrehan	if (in) {
83221828Sgrehan		nread = read(conn_fd, &ch, 1);
84221828Sgrehan		if (nread == -1 && errno == EAGAIN)
85221828Sgrehan			*eax = -1;
86221828Sgrehan		else if (nread == 1)
87221828Sgrehan			*eax = ch;
88221828Sgrehan		else {
89221828Sgrehan			close(conn_fd);
90221828Sgrehan			conn_fd = -1;
91221828Sgrehan			goto again;
92221828Sgrehan		}
93221828Sgrehan	} else {
94221828Sgrehan		ch = *eax;
95221828Sgrehan		nwritten = write(conn_fd, &ch, 1);
96221828Sgrehan		if (nwritten != 1) {
97221828Sgrehan			close(conn_fd);
98221828Sgrehan			conn_fd = -1;
99221828Sgrehan			goto again;
100221828Sgrehan		}
101221828Sgrehan	}
102221828Sgrehan	return (0);
103221828Sgrehan}
104221828Sgrehan
105242195Sneelstatic struct inout_port dbgport = {
106242195Sneel	"bvmdbg",
107242195Sneel	BVM_DBG_PORT,
108249321Sneel	1,
109242195Sneel	IOPORT_F_INOUT,
110242195Sneel	dbg_handler
111242195Sneel};
112242195Sneel
113242195Sneelvoid
114242195Sneelinit_dbgport(int sport)
115242195Sneel{
116242195Sneel	conn_fd = -1;
117242195Sneel
118242195Sneel	if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
119242195Sneel		perror("socket");
120242195Sneel		exit(1);
121242195Sneel	}
122242195Sneel
123242195Sneel	sin.sin_len = sizeof(sin);
124242195Sneel	sin.sin_family = AF_INET;
125242195Sneel	sin.sin_addr.s_addr = htonl(INADDR_ANY);
126242195Sneel	sin.sin_port = htons(sport);
127242195Sneel
128242195Sneel	if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
129242195Sneel		perror("bind");
130242195Sneel		exit(1);
131242195Sneel	}
132242195Sneel
133242195Sneel	if (listen(listen_fd, 1) < 0) {
134242195Sneel		perror("listen");
135242195Sneel		exit(1);
136242195Sneel	}
137242195Sneel
138242195Sneel	register_inout(&dbgport);
139242195Sneel}
140