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