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