xref: /qemu/hw/hyperv/syndbg.c (revision d45c8332)
1 /*
2  * QEMU Hyper-V Synthetic Debugging device
3  *
4  * This work is licensed under the terms of the GNU GPL, version 2 or later.
5  * See the COPYING file in the top-level directory.
6  */
7 
8 #include "qemu/ctype.h"
9 #include "qemu/osdep.h"
10 #include "qemu/error-report.h"
11 #include "qemu/main-loop.h"
12 #include "qemu/sockets.h"
13 #include "qemu-common.h"
14 #include "qapi/error.h"
15 #include "migration/vmstate.h"
16 #include "hw/qdev-properties.h"
17 #include "hw/loader.h"
18 #include "cpu.h"
19 #include "hw/hyperv/hyperv.h"
20 #include "hw/hyperv/vmbus-bridge.h"
21 #include "hw/hyperv/hyperv-proto.h"
22 #include "net/net.h"
23 #include "net/eth.h"
24 #include "net/checksum.h"
25 #include "trace.h"
26 
27 #define TYPE_HV_SYNDBG       "hv-syndbg"
28 
29 typedef struct HvSynDbg {
30     DeviceState parent_obj;
31 
32     char *host_ip;
33     uint16_t host_port;
34     bool use_hcalls;
35 
36     uint32_t target_ip;
37     struct sockaddr_in servaddr;
38     int socket;
39     bool has_data_pending;
40     uint64_t pending_page_gpa;
41 } HvSynDbg;
42 
43 #define HVSYNDBG(obj) OBJECT_CHECK(HvSynDbg, (obj), TYPE_HV_SYNDBG)
44 
45 /* returns NULL unless there is exactly one HV Synth debug device */
46 static HvSynDbg *hv_syndbg_find(void)
47 {
48     /* Returns NULL unless there is exactly one hvsd device */
49     return HVSYNDBG(object_resolve_path_type("", TYPE_HV_SYNDBG, NULL));
50 }
51 
52 static void set_pending_state(HvSynDbg *syndbg, bool has_pending)
53 {
54     hwaddr out_len;
55     void *out_data;
56 
57     syndbg->has_data_pending = has_pending;
58 
59     if (!syndbg->pending_page_gpa) {
60         return;
61     }
62 
63     out_len = 1;
64     out_data = cpu_physical_memory_map(syndbg->pending_page_gpa, &out_len, 1);
65     if (out_data) {
66         *(uint8_t *)out_data = !!has_pending;
67         cpu_physical_memory_unmap(out_data, out_len, 1, out_len);
68     }
69 }
70 
71 static bool get_udb_pkt_data(void *p, uint32_t len, uint32_t *data_ofs,
72                              uint32_t *src_ip)
73 {
74     uint32_t offset, curr_len = len;
75 
76     if (curr_len < sizeof(struct eth_header) ||
77         (be16_to_cpu(PKT_GET_ETH_HDR(p)->h_proto) != ETH_P_IP)) {
78         return false;
79     }
80     offset = sizeof(struct eth_header);
81     curr_len -= sizeof(struct eth_header);
82 
83     if (curr_len < sizeof(struct ip_header) ||
84         PKT_GET_IP_HDR(p)->ip_p != IP_PROTO_UDP) {
85         return false;
86     }
87     offset += PKT_GET_IP_HDR_LEN(p);
88     curr_len -= PKT_GET_IP_HDR_LEN(p);
89 
90     if (curr_len < sizeof(struct udp_header)) {
91         return false;
92     }
93 
94     offset += sizeof(struct udp_header);
95     *data_ofs = offset;
96     *src_ip = PKT_GET_IP_HDR(p)->ip_src;
97     return true;
98 }
99 
100 static uint16_t handle_send_msg(HvSynDbg *syndbg, uint64_t ingpa,
101                                 uint32_t count, bool is_raw,
102                                 uint32_t *pending_count)
103 {
104     uint16_t ret;
105     hwaddr data_len;
106     void *debug_data = NULL;
107     uint32_t udp_data_ofs = 0;
108     const void *pkt_data;
109     int sent_count;
110 
111     data_len = count;
112     debug_data = cpu_physical_memory_map(ingpa, &data_len, 0);
113     if (!debug_data || data_len < count) {
114         ret = HV_STATUS_INSUFFICIENT_MEMORY;
115         goto cleanup;
116     }
117 
118     if (is_raw &&
119         !get_udb_pkt_data(debug_data, count, &udp_data_ofs,
120                           &syndbg->target_ip)) {
121         ret = HV_STATUS_SUCCESS;
122         goto cleanup;
123     }
124 
125     pkt_data = (const void *)((uintptr_t)debug_data + udp_data_ofs);
126     sent_count = sendto(syndbg->socket, pkt_data, count - udp_data_ofs,
127                              MSG_NOSIGNAL, NULL, 0);
128     if (sent_count == -1) {
129         ret = HV_STATUS_INSUFFICIENT_MEMORY;
130         goto cleanup;
131     }
132 
133     *pending_count = count - (sent_count + udp_data_ofs);
134     ret = HV_STATUS_SUCCESS;
135 cleanup:
136     if (debug_data) {
137         cpu_physical_memory_unmap(debug_data, count, 0, data_len);
138     }
139 
140     return ret;
141 }
142 
143 #define UDP_PKT_HEADER_SIZE \
144     (sizeof(struct eth_header) + sizeof(struct ip_header) +\
145      sizeof(struct udp_header))
146 
147 static bool create_udp_pkt(HvSynDbg *syndbg, void *pkt, uint32_t pkt_len,
148                            void *udp_data, uint32_t udp_data_len)
149 {
150     struct udp_header *udp_part;
151 
152     if (pkt_len < (UDP_PKT_HEADER_SIZE + udp_data_len)) {
153         return false;
154     }
155 
156     /* Setup the eth */
157     memset(&PKT_GET_ETH_HDR(pkt)->h_source, 0, ETH_ALEN);
158     memset(&PKT_GET_ETH_HDR(pkt)->h_dest, 0, ETH_ALEN);
159     PKT_GET_ETH_HDR(pkt)->h_proto = cpu_to_be16(ETH_P_IP);
160 
161     /* Setup the ip */
162     PKT_GET_IP_HDR(pkt)->ip_ver_len =
163         (4 << 4) | (sizeof(struct ip_header) >> 2);
164     PKT_GET_IP_HDR(pkt)->ip_tos = 0;
165     PKT_GET_IP_HDR(pkt)->ip_id = 0;
166     PKT_GET_IP_HDR(pkt)->ip_off = 0;
167     PKT_GET_IP_HDR(pkt)->ip_ttl = 64; /* IPDEFTTL */
168     PKT_GET_IP_HDR(pkt)->ip_p = IP_PROTO_UDP;
169     PKT_GET_IP_HDR(pkt)->ip_src = syndbg->servaddr.sin_addr.s_addr;
170     PKT_GET_IP_HDR(pkt)->ip_dst = syndbg->target_ip;
171     PKT_GET_IP_HDR(pkt)->ip_len =
172         cpu_to_be16(sizeof(struct ip_header) + sizeof(struct udp_header) +
173                     udp_data_len);
174     eth_fix_ip4_checksum(PKT_GET_IP_HDR(pkt), PKT_GET_IP_HDR_LEN(pkt));
175 
176     udp_part = (struct udp_header *)((uintptr_t)pkt +
177                                      sizeof(struct eth_header) +
178                                      PKT_GET_IP_HDR_LEN(pkt));
179     udp_part->uh_sport = syndbg->servaddr.sin_port;
180     udp_part->uh_dport = syndbg->servaddr.sin_port;
181     udp_part->uh_ulen = cpu_to_be16(sizeof(struct udp_header) + udp_data_len);
182     memcpy(udp_part + 1, udp_data, udp_data_len);
183     net_checksum_calculate(pkt, UDP_PKT_HEADER_SIZE + udp_data_len, CSUM_ALL);
184     return true;
185 }
186 
187 static uint16_t handle_recv_msg(HvSynDbg *syndbg, uint64_t outgpa,
188                                 uint32_t count, bool is_raw, uint32_t options,
189                                 uint64_t timeout, uint32_t *retrieved_count)
190 {
191     uint16_t ret;
192     uint8_t data_buf[TARGET_PAGE_SIZE - UDP_PKT_HEADER_SIZE];
193     hwaddr out_len;
194     void *out_data;
195     ssize_t recv_byte_count;
196 
197     /* TODO: Handle options and timeout */
198     (void)options;
199     (void)timeout;
200 
201     if (!syndbg->has_data_pending) {
202         recv_byte_count = 0;
203     } else {
204         recv_byte_count = recv(syndbg->socket, data_buf,
205                                MIN(sizeof(data_buf), count), MSG_WAITALL);
206         if (recv_byte_count == -1) {
207             return HV_STATUS_INVALID_PARAMETER;
208         }
209     }
210 
211     if (!recv_byte_count) {
212         *retrieved_count = 0;
213         return HV_STATUS_NO_DATA;
214     }
215 
216     set_pending_state(syndbg, false);
217 
218     out_len = recv_byte_count;
219     if (is_raw) {
220         out_len += UDP_PKT_HEADER_SIZE;
221     }
222     out_data = cpu_physical_memory_map(outgpa, &out_len, 1);
223     if (!out_data) {
224         return HV_STATUS_INSUFFICIENT_MEMORY;
225     }
226 
227     if (is_raw &&
228         !create_udp_pkt(syndbg, out_data,
229                         recv_byte_count + UDP_PKT_HEADER_SIZE,
230                         data_buf, recv_byte_count)) {
231         ret = HV_STATUS_INSUFFICIENT_MEMORY;
232         goto cleanup_out_data;
233     } else if (!is_raw) {
234         memcpy(out_data, data_buf, recv_byte_count);
235     }
236 
237     *retrieved_count = recv_byte_count;
238     if (is_raw) {
239         *retrieved_count += UDP_PKT_HEADER_SIZE;
240     }
241     ret = HV_STATUS_SUCCESS;
242 
243 cleanup_out_data:
244     cpu_physical_memory_unmap(out_data, out_len, 1, out_len);
245     return ret;
246 }
247 
248 static uint16_t hv_syndbg_handler(void *context, HvSynDbgMsg *msg)
249 {
250     HvSynDbg *syndbg = context;
251     uint16_t ret = HV_STATUS_INVALID_HYPERCALL_CODE;
252 
253     switch (msg->type) {
254     case HV_SYNDBG_MSG_CONNECTION_INFO:
255         msg->u.connection_info.host_ip =
256             ntohl(syndbg->servaddr.sin_addr.s_addr);
257         msg->u.connection_info.host_port =
258             ntohs(syndbg->servaddr.sin_port);
259         ret = HV_STATUS_SUCCESS;
260         break;
261     case HV_SYNDBG_MSG_SEND:
262         ret = handle_send_msg(syndbg, msg->u.send.buf_gpa, msg->u.send.count,
263                               msg->u.send.is_raw, &msg->u.send.pending_count);
264         break;
265     case HV_SYNDBG_MSG_RECV:
266         ret = handle_recv_msg(syndbg, msg->u.recv.buf_gpa, msg->u.recv.count,
267                               msg->u.recv.is_raw, msg->u.recv.options,
268                               msg->u.recv.timeout,
269                               &msg->u.recv.retrieved_count);
270         break;
271     case HV_SYNDBG_MSG_SET_PENDING_PAGE:
272         syndbg->pending_page_gpa = msg->u.pending_page.buf_gpa;
273         ret = HV_STATUS_SUCCESS;
274         break;
275     case HV_SYNDBG_MSG_QUERY_OPTIONS:
276         msg->u.query_options.options = 0;
277         if (syndbg->use_hcalls) {
278             msg->u.query_options.options = HV_X64_SYNDBG_OPTION_USE_HCALLS;
279         }
280         ret = HV_STATUS_SUCCESS;
281         break;
282     default:
283         break;
284     }
285 
286     return ret;
287 }
288 
289 static void hv_syndbg_recv_event(void *opaque)
290 {
291     HvSynDbg *syndbg = opaque;
292     struct timeval tv;
293     fd_set rfds;
294 
295     tv.tv_sec = 0;
296     tv.tv_usec = 0;
297     FD_ZERO(&rfds);
298     FD_SET(syndbg->socket, &rfds);
299     if (select(syndbg->socket + 1, &rfds, NULL, NULL, &tv) > 0) {
300         set_pending_state(syndbg, true);
301     }
302 }
303 
304 static void hv_syndbg_realize(DeviceState *dev, Error **errp)
305 {
306     HvSynDbg *syndbg = HVSYNDBG(dev);
307 
308     if (!hv_syndbg_find()) {
309         error_setg(errp, "at most one %s device is permitted", TYPE_HV_SYNDBG);
310         return;
311     }
312 
313     if (!vmbus_bridge_find()) {
314         error_setg(errp, "%s device requires vmbus-bridge device",
315                    TYPE_HV_SYNDBG);
316         return;
317     }
318 
319     /* Parse and host_ip */
320     if (qemu_isdigit(syndbg->host_ip[0])) {
321         syndbg->servaddr.sin_addr.s_addr = inet_addr(syndbg->host_ip);
322     } else {
323         struct hostent *he = gethostbyname(syndbg->host_ip);
324         if (!he) {
325             error_setg(errp, "%s failed to resolve host name %s",
326                        TYPE_HV_SYNDBG, syndbg->host_ip);
327             return;
328         }
329         syndbg->servaddr.sin_addr = *(struct in_addr *)he->h_addr;
330     }
331 
332     syndbg->socket = socket(AF_INET, SOCK_DGRAM, 0);
333     if (syndbg->socket < 0) {
334         error_setg(errp, "%s failed to create socket", TYPE_HV_SYNDBG);
335         return;
336     }
337 
338     qemu_set_nonblock(syndbg->socket);
339 
340     syndbg->servaddr.sin_port = htons(syndbg->host_port);
341     syndbg->servaddr.sin_family = AF_INET;
342     if (connect(syndbg->socket, (struct sockaddr *)&syndbg->servaddr,
343                 sizeof(syndbg->servaddr)) < 0) {
344         closesocket(syndbg->socket);
345         error_setg(errp, "%s failed to connect to socket", TYPE_HV_SYNDBG);
346         return;
347     }
348 
349     syndbg->pending_page_gpa = 0;
350     syndbg->has_data_pending = false;
351     hyperv_set_syndbg_handler(hv_syndbg_handler, syndbg);
352     qemu_set_fd_handler(syndbg->socket, hv_syndbg_recv_event, NULL, syndbg);
353 }
354 
355 static void hv_syndbg_unrealize(DeviceState *dev)
356 {
357     HvSynDbg *syndbg = HVSYNDBG(dev);
358 
359     if (syndbg->socket > 0) {
360         qemu_set_fd_handler(syndbg->socket, NULL, NULL, NULL);
361         closesocket(syndbg->socket);
362     }
363 }
364 
365 static const VMStateDescription vmstate_hv_syndbg = {
366     .name = TYPE_HV_SYNDBG,
367     .unmigratable = 1,
368 };
369 
370 static Property hv_syndbg_properties[] = {
371     DEFINE_PROP_STRING("host_ip", HvSynDbg, host_ip),
372     DEFINE_PROP_UINT16("host_port", HvSynDbg, host_port, 50000),
373     DEFINE_PROP_BOOL("use_hcalls", HvSynDbg, use_hcalls, false),
374     DEFINE_PROP_END_OF_LIST(),
375 };
376 
377 static void hv_syndbg_class_init(ObjectClass *klass, void *data)
378 {
379     DeviceClass *dc = DEVICE_CLASS(klass);
380 
381     device_class_set_props(dc, hv_syndbg_properties);
382     dc->fw_name = TYPE_HV_SYNDBG;
383     dc->vmsd = &vmstate_hv_syndbg;
384     dc->realize = hv_syndbg_realize;
385     dc->unrealize = hv_syndbg_unrealize;
386     dc->user_creatable = true;
387     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
388 }
389 
390 static const TypeInfo hv_syndbg_type_info = {
391     .name = TYPE_HV_SYNDBG,
392     .parent = TYPE_DEVICE,
393     .instance_size = sizeof(HvSynDbg),
394     .class_init = hv_syndbg_class_init,
395 };
396 
397 static void hv_syndbg_register_types(void)
398 {
399     type_register_static(&hv_syndbg_type_info);
400 }
401 
402 type_init(hv_syndbg_register_types)
403