]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/dbgport.c
IFC to head to catch up the bhyve branch
[FreeBSD/FreeBSD.git] / usr.sbin / bhyve / dbgport.c
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
48 static int listen_fd, conn_fd;
49
50 static struct sockaddr_in sin;
51
52 void
53 init_dbgport(int sport)
54 {
55         conn_fd = -1;
56
57         if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
58                 perror("socket");
59                 exit(1);
60         }
61
62         sin.sin_len = sizeof(sin);
63         sin.sin_family = AF_INET;
64         sin.sin_addr.s_addr = htonl(INADDR_ANY);
65         sin.sin_port = htons(sport);
66
67         if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
68                 perror("bind");
69                 exit(1);
70         }
71
72         if (listen(listen_fd, 1) < 0) {
73                 perror("listen");
74                 exit(1);
75         }
76 }
77
78 static int
79 dbg_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
80             uint32_t *eax, void *arg)
81 {
82         char ch;
83         int nwritten, nread, printonce;
84
85         if (bytes != 4)
86                 return (-1);
87
88 again:
89         printonce = 0;
90         while (conn_fd < 0) {
91                 if (!printonce) {
92                         printf("Waiting for connection from gdb\r\n");
93                         printonce = 1;
94                 }
95                 conn_fd = accept(listen_fd, NULL, NULL);
96                 if (conn_fd >= 0)
97                         fcntl(conn_fd, F_SETFL, O_NONBLOCK);
98                 else if (errno != EINTR)
99                         perror("accept");
100         }
101
102         if (in) {
103                 nread = read(conn_fd, &ch, 1);
104                 if (nread == -1 && errno == EAGAIN)
105                         *eax = -1;
106                 else if (nread == 1)
107                         *eax = ch;
108                 else {
109                         close(conn_fd);
110                         conn_fd = -1;
111                         goto again;
112                 }
113         } else {
114                 ch = *eax;
115                 nwritten = write(conn_fd, &ch, 1);
116                 if (nwritten != 1) {
117                         close(conn_fd);
118                         conn_fd = -1;
119                         goto again;
120                 }
121         }
122         return (0);
123 }
124
125 INOUT_PORT(dbg, BVM_DBG_PORT, IOPORT_F_INOUT, dbg_handler);