]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/dbgport.c
MFV r317781:
[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 #ifndef WITHOUT_CAPSICUM
34 #include <sys/capsicum.h>
35 #endif
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <netinet/tcp.h>
39 #include <sys/uio.h>
40
41 #include <err.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <sysexits.h>
45 #include <fcntl.h>
46 #include <unistd.h>
47 #include <errno.h>
48
49 #include "inout.h"
50 #include "dbgport.h"
51 #include "pci_lpc.h"
52
53 #define BVM_DBG_PORT    0x224
54 #define BVM_DBG_SIG     ('B' << 8 | 'V')
55
56 static int listen_fd, conn_fd;
57
58 static struct sockaddr_in sin;
59
60 static int
61 dbg_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
62             uint32_t *eax, void *arg)
63 {
64         int nwritten, nread, printonce;
65         int on = 1;
66         char ch;
67
68         if (bytes == 2 && in) {
69                 *eax = BVM_DBG_SIG;
70                 return (0);
71         }
72
73         if (bytes != 4)
74                 return (-1);
75
76 again:
77         printonce = 0;
78         while (conn_fd < 0) {
79                 if (!printonce) {
80                         printf("Waiting for connection from gdb\r\n");
81                         printonce = 1;
82                 }
83                 conn_fd = accept4(listen_fd, NULL, NULL, SOCK_NONBLOCK);
84                 if (conn_fd >= 0) {
85                         /* Avoid EPIPE after the client drops off. */
86                         (void)setsockopt(conn_fd, SOL_SOCKET, SO_NOSIGPIPE,
87                             &on, sizeof(on));
88                         /* Improve latency for one byte at a time tranfers. */
89                         (void)setsockopt(conn_fd, IPPROTO_TCP, TCP_NODELAY,
90                             &on, sizeof(on));
91                 } else if (errno != EINTR) {
92                         perror("accept");
93                 }
94         }
95
96         if (in) {
97                 nread = read(conn_fd, &ch, 1);
98                 if (nread == -1 && errno == EAGAIN)
99                         *eax = -1;
100                 else if (nread == 1)
101                         *eax = ch;
102                 else {
103                         close(conn_fd);
104                         conn_fd = -1;
105                         goto again;
106                 }
107         } else {
108                 ch = *eax;
109                 nwritten = write(conn_fd, &ch, 1);
110                 if (nwritten != 1) {
111                         close(conn_fd);
112                         conn_fd = -1;
113                         goto again;
114                 }
115         }
116         return (0);
117 }
118
119 static struct inout_port dbgport = {
120         "bvmdbg",
121         BVM_DBG_PORT,
122         1,
123         IOPORT_F_INOUT,
124         dbg_handler
125 };
126
127 SYSRES_IO(BVM_DBG_PORT, 4);
128
129 void
130 init_dbgport(int sport)
131 {
132         int reuse;
133 #ifndef WITHOUT_CAPSICUM
134         cap_rights_t rights;
135 #endif
136
137         conn_fd = -1;
138
139         if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
140                 perror("socket");
141                 exit(1);
142         }
143
144         sin.sin_len = sizeof(sin);
145         sin.sin_family = AF_INET;
146         sin.sin_addr.s_addr = htonl(INADDR_ANY);
147         sin.sin_port = htons(sport);
148
149         reuse = 1;
150         if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse,
151             sizeof(reuse)) < 0) {
152                 perror("setsockopt");
153                 exit(1);
154         }
155
156         if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
157                 perror("bind");
158                 exit(1);
159         }
160
161         if (listen(listen_fd, 1) < 0) {
162                 perror("listen");
163                 exit(1);
164         }
165
166 #ifndef WITHOUT_CAPSICUM
167         cap_rights_init(&rights, CAP_ACCEPT, CAP_READ, CAP_WRITE);
168         if (cap_rights_limit(listen_fd, &rights) == -1 && errno != ENOSYS)
169                 errx(EX_OSERR, "Unable to apply rights for sandbox");
170 #endif
171
172         register_inout(&dbgport);
173 }