]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/dbgport.c
sys/{x86,amd64}: remove one of doubled ;s
[FreeBSD/FreeBSD.git] / usr.sbin / bhyve / dbgport.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 NetApp, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/types.h>
35 #ifndef WITHOUT_CAPSICUM
36 #include <sys/capsicum.h>
37 #endif
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <netinet/tcp.h>
41 #include <sys/uio.h>
42
43 #ifndef WITHOUT_CAPSICUM
44 #include <capsicum_helpers.h>
45 #endif
46 #include <err.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <sysexits.h>
50 #include <fcntl.h>
51 #include <unistd.h>
52 #include <errno.h>
53
54 #include "inout.h"
55 #include "dbgport.h"
56 #include "pci_lpc.h"
57
58 #define BVM_DBG_PORT    0x224
59 #define BVM_DBG_SIG     ('B' << 8 | 'V')
60
61 static int listen_fd, conn_fd;
62
63 static struct sockaddr_in sin;
64
65 static int
66 dbg_handler(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
67             uint32_t *eax, void *arg)
68 {
69         int nwritten, nread, printonce;
70         int on = 1;
71         char ch;
72
73         if (bytes == 2 && in) {
74                 *eax = BVM_DBG_SIG;
75                 return (0);
76         }
77
78         if (bytes != 4)
79                 return (-1);
80
81 again:
82         printonce = 0;
83         while (conn_fd < 0) {
84                 if (!printonce) {
85                         printf("Waiting for connection from gdb\r\n");
86                         printonce = 1;
87                 }
88                 conn_fd = accept4(listen_fd, NULL, NULL, SOCK_NONBLOCK);
89                 if (conn_fd >= 0) {
90                         /* Avoid EPIPE after the client drops off. */
91                         (void)setsockopt(conn_fd, SOL_SOCKET, SO_NOSIGPIPE,
92                             &on, sizeof(on));
93                         /* Improve latency for one byte at a time tranfers. */
94                         (void)setsockopt(conn_fd, IPPROTO_TCP, TCP_NODELAY,
95                             &on, sizeof(on));
96                 } else if (errno != EINTR) {
97                         perror("accept");
98                 }
99         }
100
101         if (in) {
102                 nread = read(conn_fd, &ch, 1);
103                 if (nread == -1 && errno == EAGAIN)
104                         *eax = -1;
105                 else if (nread == 1)
106                         *eax = ch;
107                 else {
108                         close(conn_fd);
109                         conn_fd = -1;
110                         goto again;
111                 }
112         } else {
113                 ch = *eax;
114                 nwritten = write(conn_fd, &ch, 1);
115                 if (nwritten != 1) {
116                         close(conn_fd);
117                         conn_fd = -1;
118                         goto again;
119                 }
120         }
121         return (0);
122 }
123
124 static struct inout_port dbgport = {
125         "bvmdbg",
126         BVM_DBG_PORT,
127         1,
128         IOPORT_F_INOUT,
129         dbg_handler
130 };
131
132 SYSRES_IO(BVM_DBG_PORT, 4);
133
134 void
135 init_dbgport(int sport)
136 {
137         int reuse;
138 #ifndef WITHOUT_CAPSICUM
139         cap_rights_t rights;
140 #endif
141
142         conn_fd = -1;
143
144         if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
145                 perror("cannot create socket");
146                 exit(4);
147         }
148
149         sin.sin_len = sizeof(sin);
150         sin.sin_family = AF_INET;
151         sin.sin_addr.s_addr = htonl(INADDR_ANY);
152         sin.sin_port = htons(sport);
153
154         reuse = 1;
155         if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse,
156             sizeof(reuse)) < 0) {
157                 perror("cannot set socket options");
158                 exit(4);
159         }
160
161         if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
162                 perror("cannot bind socket");
163                 exit(4);
164         }
165
166         if (listen(listen_fd, 1) < 0) {
167                 perror("cannot listen socket");
168                 exit(4);
169         }
170
171 #ifndef WITHOUT_CAPSICUM
172         cap_rights_init(&rights, CAP_ACCEPT, CAP_READ, CAP_WRITE);
173         if (caph_rights_limit(listen_fd, &rights) == -1)
174                 errx(EX_OSERR, "Unable to apply rights for sandbox");
175 #endif
176
177         register_inout(&dbgport);
178 }