master
c 998 lines 27.3 KB
Raw
1 /*
2 * Hyper-V guest/hypervisor interaction
3 *
4 * Copyright (c) 2015-2018 Virtuozzo International GmbH.
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 */
9
10 #include "qemu/osdep.h"
11 #include "qemu/main-loop.h"
12 #include "qemu/module.h"
13 #include "qapi/error.h"
14 #include "system/address-spaces.h"
15 #include "system/memory.h"
16 #include "exec/target_page.h"
17 #include "exec/cpu-common.h"
18 #include "linux/kvm.h"
19 #include "system/kvm.h"
20 #include "system/physmem.h"
21 #include "qemu/bitops.h"
22 #include "qemu/error-report.h"
23 #include "qemu/lockable.h"
24 #include "qemu/queue.h"
25 #include "qemu/rcu.h"
26 #include "qemu/rcu_queue.h"
27 #include "hw/hyperv/hyperv.h"
28 #include "qom/object.h"
29 #include "target/i386/kvm/hyperv-proto.h"
30
31 struct SynICState {
32 DeviceState parent_obj;
33
34 CPUState *cs;
35
36 bool sctl_enabled;
37 hwaddr msg_page_addr;
38 hwaddr event_page_addr;
39 MemoryRegion msg_page_mr;
40 MemoryRegion event_page_mr;
41 struct hyperv_message_page *msg_page;
42 struct hyperv_event_flags_page *event_page;
43
44 QemuMutex sint_routes_mutex;
45 QLIST_HEAD(, HvSintRoute) sint_routes;
46 };
47
48 #define TYPE_SYNIC "hyperv-synic"
49 OBJECT_DECLARE_SIMPLE_TYPE(SynICState, SYNIC)
50
51 static bool synic_enabled;
52
53 bool hyperv_is_synic_enabled(void)
54 {
55 return synic_enabled;
56 }
57
58 static SynICState *get_synic(CPUState *cs)
59 {
60 return SYNIC(object_resolve_path_component(OBJECT(cs), "synic"));
61 }
62
63 bool hyperv_is_synic_present(CPUState *cs)
64 {
65 return get_synic(cs);
66 }
67
68 static void synic_update(SynICState *synic, bool sctl_enable,
69 hwaddr msg_page_addr, hwaddr event_page_addr)
70 {
71
72 synic->sctl_enabled = sctl_enable;
73 if (synic->msg_page_addr != msg_page_addr) {
74 if (synic->msg_page_addr) {
75 memory_region_del_subregion(get_system_memory(),
76 &synic->msg_page_mr);
77 }
78 if (msg_page_addr) {
79 memory_region_add_subregion(get_system_memory(), msg_page_addr,
80 &synic->msg_page_mr);
81 }
82 synic->msg_page_addr = msg_page_addr;
83 }
84 if (synic->event_page_addr != event_page_addr) {
85 if (synic->event_page_addr) {
86 memory_region_del_subregion(get_system_memory(),
87 &synic->event_page_mr);
88 }
89 if (event_page_addr) {
90 memory_region_add_subregion(get_system_memory(), event_page_addr,
91 &synic->event_page_mr);
92 }
93 synic->event_page_addr = event_page_addr;
94 }
95 }
96
97 void hyperv_synic_update(CPUState *cs, bool sctl_enable,
98 hwaddr msg_page_addr, hwaddr event_page_addr)
99 {
100 SynICState *synic = get_synic(cs);
101
102 if (!synic) {
103 return;
104 }
105
106 synic_update(synic, sctl_enable, msg_page_addr, event_page_addr);
107 }
108
109 static void synic_realize(DeviceState *dev, Error **errp)
110 {
111 Object *obj = OBJECT(dev);
112 SynICState *synic = SYNIC(dev);
113 char *msgp_name, *eventp_name;
114 uint32_t vp_index;
115
116 /* memory region names have to be globally unique */
117 vp_index = hyperv_vp_index(synic->cs);
118 msgp_name = g_strdup_printf("synic-%u-msg-page", vp_index);
119 eventp_name = g_strdup_printf("synic-%u-event-page", vp_index);
120
121 memory_region_init_ram(&synic->msg_page_mr, obj, msgp_name,
122 sizeof(*synic->msg_page), &error_abort);
123 memory_region_init_ram(&synic->event_page_mr, obj, eventp_name,
124 sizeof(*synic->event_page), &error_abort);
125 synic->msg_page = memory_region_get_ram_ptr(&synic->msg_page_mr);
126 synic->event_page = memory_region_get_ram_ptr(&synic->event_page_mr);
127 qemu_mutex_init(&synic->sint_routes_mutex);
128 QLIST_INIT(&synic->sint_routes);
129
130 g_free(msgp_name);
131 g_free(eventp_name);
132 }
133
134 static void synic_reset(DeviceState *dev)
135 {
136 SynICState *synic = SYNIC(dev);
137 memset(synic->msg_page, 0, sizeof(*synic->msg_page));
138 memset(synic->event_page, 0, sizeof(*synic->event_page));
139 synic_update(synic, false, 0, 0);
140 assert(QLIST_EMPTY(&synic->sint_routes));
141 }
142
143 static void synic_class_init(ObjectClass *klass, const void *data)
144 {
145 DeviceClass *dc = DEVICE_CLASS(klass);
146
147 dc->realize = synic_realize;
148 device_class_set_legacy_reset(dc, synic_reset);
149 dc->user_creatable = false;
150 }
151
152 void hyperv_synic_add(CPUState *cs)
153 {
154 Object *obj;
155 SynICState *synic;
156
157 obj = object_new(TYPE_SYNIC);
158 synic = SYNIC(obj);
159 synic->cs = cs;
160 object_property_add_child(OBJECT(cs), "synic", obj);
161 object_unref(obj);
162 qdev_realize(DEVICE(obj), NULL, &error_abort);
163 synic_enabled = true;
164 }
165
166 void hyperv_synic_reset(CPUState *cs)
167 {
168 SynICState *synic = get_synic(cs);
169
170 if (synic) {
171 device_cold_reset(DEVICE(synic));
172 }
173 }
174
175 static const TypeInfo synic_type_info = {
176 .name = TYPE_SYNIC,
177 .parent = TYPE_DEVICE,
178 .instance_size = sizeof(SynICState),
179 .class_init = synic_class_init,
180 };
181
182 static void synic_register_types(void)
183 {
184 type_register_static(&synic_type_info);
185 }
186
187 type_init(synic_register_types)
188
189 /*
190 * KVM has its own message producers (SynIC timers). To guarantee
191 * serialization with both KVM vcpu and the guest cpu, the messages are first
192 * staged in an intermediate area and then posted to the SynIC message page in
193 * the vcpu thread.
194 */
195 typedef struct HvSintStagedMessage {
196 /* message content staged by hyperv_post_msg */
197 struct hyperv_message msg;
198 /* callback + data (r/o) to complete the processing in a BH */
199 HvSintMsgCb cb;
200 void *cb_data;
201 /* message posting status filled by cpu_post_msg */
202 int status;
203 /* passing the buck: */
204 enum {
205 /* initial state */
206 HV_STAGED_MSG_FREE,
207 /*
208 * hyperv_post_msg (e.g. in main loop) grabs the staged area (FREE ->
209 * BUSY), copies msg, and schedules cpu_post_msg on the assigned cpu
210 */
211 HV_STAGED_MSG_BUSY,
212 /*
213 * cpu_post_msg (vcpu thread) tries to copy staged msg to msg slot,
214 * notify the guest, records the status, marks the posting done (BUSY
215 * -> POSTED), and schedules sint_msg_bh BH
216 */
217 HV_STAGED_MSG_POSTED,
218 /*
219 * sint_msg_bh (BH) verifies that the posting is done, runs the
220 * callback, and starts over (POSTED -> FREE)
221 */
222 } state;
223 } HvSintStagedMessage;
224
225 struct HvSintRoute {
226 uint32_t sint;
227 SynICState *synic;
228 int gsi;
229 EventNotifier sint_set_notifier;
230 EventNotifier sint_ack_notifier;
231
232 HvSintStagedMessage *staged_msg;
233
234 unsigned refcount;
235 QLIST_ENTRY(HvSintRoute) link;
236 };
237
238 static CPUState *hyperv_find_vcpu(uint32_t vp_index)
239 {
240 return qemu_get_cpu(vp_index);
241 }
242
243 /*
244 * BH to complete the processing of a staged message.
245 */
246 static void sint_msg_bh(void *opaque)
247 {
248 HvSintRoute *sint_route = opaque;
249 HvSintStagedMessage *staged_msg = sint_route->staged_msg;
250
251 if (qatomic_read(&staged_msg->state) != HV_STAGED_MSG_POSTED) {
252 /* status nor ready yet (spurious ack from guest?), ignore */
253 return;
254 }
255
256 staged_msg->cb(staged_msg->cb_data, staged_msg->status);
257 staged_msg->status = 0;
258
259 /* staged message processing finished, ready to start over */
260 qatomic_set(&staged_msg->state, HV_STAGED_MSG_FREE);
261 /* drop the reference taken in hyperv_post_msg */
262 hyperv_sint_route_unref(sint_route);
263 }
264
265 /*
266 * Worker to transfer the message from the staging area into the SynIC message
267 * page in vcpu context.
268 */
269 static void cpu_post_msg(CPUState *cs, run_on_cpu_data data)
270 {
271 HvSintRoute *sint_route = data.host_ptr;
272 HvSintStagedMessage *staged_msg = sint_route->staged_msg;
273 SynICState *synic = sint_route->synic;
274 struct hyperv_message *dst_msg;
275 bool wait_for_sint_ack = false;
276
277 assert(staged_msg->state == HV_STAGED_MSG_BUSY);
278
279 if (!synic->msg_page_addr) {
280 staged_msg->status = -ENXIO;
281 goto posted;
282 }
283
284 dst_msg = &synic->msg_page->slot[sint_route->sint];
285
286 if (dst_msg->header.message_type != HV_MESSAGE_NONE) {
287 dst_msg->header.message_flags |= HV_MESSAGE_FLAG_PENDING;
288 staged_msg->status = -EAGAIN;
289 wait_for_sint_ack = true;
290 } else {
291 memcpy(dst_msg, &staged_msg->msg, sizeof(*dst_msg));
292 staged_msg->status = hyperv_sint_route_set_sint(sint_route);
293 }
294
295 memory_region_set_dirty(&synic->msg_page_mr, 0, sizeof(*synic->msg_page));
296
297 posted:
298 qatomic_set(&staged_msg->state, HV_STAGED_MSG_POSTED);
299 /*
300 * Notify the msg originator of the progress made; if the slot was busy we
301 * set msg_pending flag in it so it will be the guest who will do EOM and
302 * trigger the notification from KVM via sint_ack_notifier
303 */
304 if (!wait_for_sint_ack) {
305 aio_bh_schedule_oneshot(qemu_get_aio_context(), sint_msg_bh,
306 sint_route);
307 }
308 }
309
310 /*
311 * Post a Hyper-V message to the staging area, for delivery to guest in the
312 * vcpu thread.
313 */
314 int hyperv_post_msg(HvSintRoute *sint_route, struct hyperv_message *src_msg)
315 {
316 HvSintStagedMessage *staged_msg = sint_route->staged_msg;
317
318 assert(staged_msg);
319
320 /* grab the staging area */
321 if (qatomic_cmpxchg(&staged_msg->state, HV_STAGED_MSG_FREE,
322 HV_STAGED_MSG_BUSY) != HV_STAGED_MSG_FREE) {
323 return -EAGAIN;
324 }
325
326 memcpy(&staged_msg->msg, src_msg, sizeof(*src_msg));
327
328 /* hold a reference on sint_route until the callback is finished */
329 hyperv_sint_route_ref(sint_route);
330
331 /* schedule message posting attempt in vcpu thread */
332 async_run_on_cpu(sint_route->synic->cs, cpu_post_msg,
333 RUN_ON_CPU_HOST_PTR(sint_route));
334 return 0;
335 }
336
337 static void sint_ack_handler(EventNotifier *notifier)
338 {
339 HvSintRoute *sint_route = container_of(notifier, HvSintRoute,
340 sint_ack_notifier);
341 event_notifier_test_and_clear(notifier);
342
343 /*
344 * the guest consumed the previous message so complete the current one with
345 * -EAGAIN and let the msg originator retry
346 */
347 aio_bh_schedule_oneshot(qemu_get_aio_context(), sint_msg_bh, sint_route);
348 }
349
350 /*
351 * Set given event flag for a given sint on a given vcpu, and signal the sint.
352 */
353 int hyperv_set_event_flag(HvSintRoute *sint_route, unsigned eventno)
354 {
355 int ret;
356 SynICState *synic = sint_route->synic;
357 unsigned long *flags, set_mask;
358 unsigned set_idx;
359
360 if (eventno > HV_EVENT_FLAGS_COUNT) {
361 return -EINVAL;
362 }
363 if (!synic->sctl_enabled || !synic->event_page_addr) {
364 return -ENXIO;
365 }
366
367 set_idx = BIT_WORD(eventno);
368 set_mask = BIT_MASK(eventno);
369 flags = synic->event_page->slot[sint_route->sint].flags;
370
371 if ((qatomic_fetch_or(&flags[set_idx], set_mask) & set_mask) != set_mask) {
372 memory_region_set_dirty(&synic->event_page_mr, 0,
373 sizeof(*synic->event_page));
374 ret = hyperv_sint_route_set_sint(sint_route);
375 } else {
376 ret = 0;
377 }
378 return ret;
379 }
380
381 static int kvm_irqchip_add_hv_sint_route(KVMState *s, uint32_t vcpu, uint32_t sint)
382 {
383 struct kvm_irq_routing_entry kroute = {};
384 int virq;
385
386 if (!kvm_gsi_routing_enabled()) {
387 return -ENOSYS;
388 }
389 virq = kvm_irqchip_get_virq(s);
390 if (virq < 0) {
391 return virq;
392 }
393
394 kroute.gsi = virq;
395 kroute.type = KVM_IRQ_ROUTING_HV_SINT;
396 kroute.flags = 0;
397 kroute.u.hv_sint.vcpu = vcpu;
398 kroute.u.hv_sint.sint = sint;
399
400 kvm_add_routing_entry(s, &kroute);
401 kvm_irqchip_commit_routes(s);
402
403 return virq;
404 }
405
406 HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint,
407 HvSintMsgCb cb, void *cb_data)
408 {
409 HvSintRoute *sint_route = NULL;
410 EventNotifier *ack_notifier = NULL;
411 int r, gsi;
412 CPUState *cs;
413 SynICState *synic;
414 bool ack_event_initialized = false;
415
416 cs = hyperv_find_vcpu(vp_index);
417 if (!cs) {
418 return NULL;
419 }
420
421 synic = get_synic(cs);
422 if (!synic) {
423 return NULL;
424 }
425
426 sint_route = g_new0(HvSintRoute, 1);
427 if (!sint_route) {
428 return NULL;
429 }
430
431 sint_route->synic = synic;
432 sint_route->sint = sint;
433 sint_route->refcount = 1;
434
435 ack_notifier = cb ? &sint_route->sint_ack_notifier : NULL;
436 if (ack_notifier) {
437 sint_route->staged_msg = g_new0(HvSintStagedMessage, 1);
438 if (!sint_route->staged_msg) {
439 goto cleanup_err_sint;
440 }
441 sint_route->staged_msg->cb = cb;
442 sint_route->staged_msg->cb_data = cb_data;
443
444 r = event_notifier_init(ack_notifier, false);
445 if (r < 0) {
446 goto cleanup_err_sint;
447 }
448 event_notifier_set_handler(ack_notifier, sint_ack_handler);
449 ack_event_initialized = true;
450 }
451
452 /* See if we are done or we need to setup a GSI for this SintRoute */
453 if (!synic->sctl_enabled) {
454 goto cleanup;
455 }
456
457 /* We need to setup a GSI for this SintRoute */
458 r = event_notifier_init(&sint_route->sint_set_notifier, false);
459 if (r < 0) {
460 goto cleanup_err_sint;
461 }
462
463 gsi = kvm_irqchip_add_hv_sint_route(kvm_state, vp_index, sint);
464 if (gsi < 0) {
465 goto cleanup_err_sint_notifier;
466 }
467
468 r = kvm_irqchip_add_irqfd_notifier_gsi(kvm_state,
469 &sint_route->sint_set_notifier,
470 ack_notifier, gsi);
471 if (r) {
472 goto cleanup_err_irqfd;
473 }
474 sint_route->gsi = gsi;
475 cleanup:
476 qemu_mutex_lock(&synic->sint_routes_mutex);
477 QLIST_INSERT_HEAD(&synic->sint_routes, sint_route, link);
478 qemu_mutex_unlock(&synic->sint_routes_mutex);
479 return sint_route;
480
481 cleanup_err_irqfd:
482 kvm_irqchip_release_virq(kvm_state, gsi);
483
484 cleanup_err_sint_notifier:
485 event_notifier_cleanup(&sint_route->sint_set_notifier);
486
487 cleanup_err_sint:
488 if (ack_notifier) {
489 if (ack_event_initialized) {
490 event_notifier_set_handler(ack_notifier, NULL);
491 event_notifier_cleanup(ack_notifier);
492 }
493
494 g_free(sint_route->staged_msg);
495 }
496
497 g_free(sint_route);
498 return NULL;
499 }
500
501 void hyperv_sint_route_ref(HvSintRoute *sint_route)
502 {
503 sint_route->refcount++;
504 }
505
506 void hyperv_sint_route_unref(HvSintRoute *sint_route)
507 {
508 SynICState *synic;
509
510 if (!sint_route) {
511 return;
512 }
513
514 assert(sint_route->refcount > 0);
515
516 if (--sint_route->refcount) {
517 return;
518 }
519
520 synic = sint_route->synic;
521 qemu_mutex_lock(&synic->sint_routes_mutex);
522 QLIST_REMOVE(sint_route, link);
523 qemu_mutex_unlock(&synic->sint_routes_mutex);
524
525 if (sint_route->gsi) {
526 kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state,
527 &sint_route->sint_set_notifier,
528 sint_route->gsi);
529 kvm_irqchip_release_virq(kvm_state, sint_route->gsi);
530 event_notifier_cleanup(&sint_route->sint_set_notifier);
531 }
532
533 if (sint_route->staged_msg) {
534 event_notifier_set_handler(&sint_route->sint_ack_notifier, NULL);
535 event_notifier_cleanup(&sint_route->sint_ack_notifier);
536 g_free(sint_route->staged_msg);
537 }
538 g_free(sint_route);
539 }
540
541 int hyperv_sint_route_set_sint(HvSintRoute *sint_route)
542 {
543 if (!sint_route->gsi) {
544 return 0;
545 }
546
547 return event_notifier_set(&sint_route->sint_set_notifier);
548 }
549
550 typedef struct MsgHandler {
551 struct rcu_head rcu;
552 QLIST_ENTRY(MsgHandler) link;
553 uint32_t conn_id;
554 HvMsgHandler handler;
555 void *data;
556 } MsgHandler;
557
558 typedef struct EventFlagHandler {
559 struct rcu_head rcu;
560 QLIST_ENTRY(EventFlagHandler) link;
561 uint32_t conn_id;
562 EventNotifier *notifier;
563 } EventFlagHandler;
564
565 static QLIST_HEAD(, MsgHandler) msg_handlers;
566 static QLIST_HEAD(, EventFlagHandler) event_flag_handlers;
567 static QemuMutex handlers_mutex;
568
569 static void __attribute__((constructor)) hv_init(void)
570 {
571 QLIST_INIT(&msg_handlers);
572 QLIST_INIT(&event_flag_handlers);
573 qemu_mutex_init(&handlers_mutex);
574 }
575
576 int hyperv_set_msg_handler(uint32_t conn_id, HvMsgHandler handler, void *data)
577 {
578 int ret;
579 MsgHandler *mh;
580
581 QEMU_LOCK_GUARD(&handlers_mutex);
582 QLIST_FOREACH(mh, &msg_handlers, link) {
583 if (mh->conn_id == conn_id) {
584 if (handler) {
585 ret = -EEXIST;
586 } else {
587 QLIST_REMOVE_RCU(mh, link);
588 g_free_rcu(mh, rcu);
589 ret = 0;
590 }
591 return ret;
592 }
593 }
594
595 if (handler) {
596 mh = g_new(MsgHandler, 1);
597 mh->conn_id = conn_id;
598 mh->handler = handler;
599 mh->data = data;
600 QLIST_INSERT_HEAD_RCU(&msg_handlers, mh, link);
601 ret = 0;
602 } else {
603 ret = -ENOENT;
604 }
605
606 return ret;
607 }
608
609 uint16_t hyperv_hcall_post_message(uint64_t param, bool fast)
610 {
611 uint16_t ret;
612 hwaddr len;
613 struct hyperv_post_message_input *msg;
614 MsgHandler *mh;
615
616 if (fast) {
617 return HV_STATUS_INVALID_HYPERCALL_CODE;
618 }
619 if (param & (__alignof__(*msg) - 1)) {
620 return HV_STATUS_INVALID_ALIGNMENT;
621 }
622
623 len = sizeof(*msg);
624 msg = physical_memory_map(param, &len, 0);
625 if (len < sizeof(*msg)) {
626 ret = HV_STATUS_INSUFFICIENT_MEMORY;
627 goto unmap;
628 }
629 if (msg->payload_size > sizeof(msg->payload)) {
630 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
631 goto unmap;
632 }
633
634 ret = HV_STATUS_INVALID_CONNECTION_ID;
635 WITH_RCU_READ_LOCK_GUARD() {
636 QLIST_FOREACH_RCU(mh, &msg_handlers, link) {
637 if (mh->conn_id == (msg->connection_id & HV_CONNECTION_ID_MASK)) {
638 ret = mh->handler(msg, mh->data);
639 break;
640 }
641 }
642 }
643
644 unmap:
645 physical_memory_unmap(msg, len, 0, 0);
646 return ret;
647 }
648
649 static int set_event_flag_handler(uint32_t conn_id, EventNotifier *notifier)
650 {
651 int ret;
652 EventFlagHandler *handler;
653
654 QEMU_LOCK_GUARD(&handlers_mutex);
655 QLIST_FOREACH(handler, &event_flag_handlers, link) {
656 if (handler->conn_id == conn_id) {
657 if (notifier) {
658 ret = -EEXIST;
659 } else {
660 QLIST_REMOVE_RCU(handler, link);
661 g_free_rcu(handler, rcu);
662 ret = 0;
663 }
664 return ret;
665 }
666 }
667
668 if (notifier) {
669 handler = g_new(EventFlagHandler, 1);
670 handler->conn_id = conn_id;
671 handler->notifier = notifier;
672 QLIST_INSERT_HEAD_RCU(&event_flag_handlers, handler, link);
673 ret = 0;
674 } else {
675 ret = -ENOENT;
676 }
677
678 return ret;
679 }
680
681 static bool process_event_flags_userspace;
682
683 int hyperv_set_event_flag_handler(uint32_t conn_id, EventNotifier *notifier)
684 {
685 if (!process_event_flags_userspace &&
686 !kvm_check_extension(kvm_state, KVM_CAP_HYPERV_EVENTFD)) {
687 process_event_flags_userspace = true;
688
689 warn_report("Hyper-V event signaling is not supported by this kernel; "
690 "using slower userspace hypercall processing");
691 }
692
693 if (!process_event_flags_userspace) {
694 struct kvm_hyperv_eventfd hvevfd = {
695 .conn_id = conn_id,
696 .fd = notifier ? event_notifier_get_fd(notifier) : -1,
697 .flags = notifier ? 0 : KVM_HYPERV_EVENTFD_DEASSIGN,
698 };
699
700 return kvm_vm_ioctl(kvm_state, KVM_HYPERV_EVENTFD, &hvevfd);
701 }
702 return set_event_flag_handler(conn_id, notifier);
703 }
704
705 uint16_t hyperv_hcall_signal_event(uint64_t param, bool fast)
706 {
707 EventFlagHandler *handler;
708
709 if (unlikely(!fast)) {
710 MemTxResult result;
711 hwaddr addr = param;
712
713 if (addr & (__alignof__(addr) - 1)) {
714 return HV_STATUS_INVALID_ALIGNMENT;
715 }
716
717 param = address_space_ldq_le(&address_space_memory, addr,
718 MEMTXATTRS_UNSPECIFIED, &result);
719 assert(result == MEMTX_OK);
720 }
721
722 /*
723 * Per spec, bits 32-47 contain the extra "flag number". However, we
724 * have no use for it, and in all known usecases it is zero, so just
725 * report lookup failure if it isn't.
726 */
727 if (param & 0xffff00000000ULL) {
728 return HV_STATUS_INVALID_PORT_ID;
729 }
730 /* remaining bits are reserved-zero */
731 if (param & ~HV_CONNECTION_ID_MASK) {
732 return HV_STATUS_INVALID_HYPERCALL_INPUT;
733 }
734
735 RCU_READ_LOCK_GUARD();
736 QLIST_FOREACH_RCU(handler, &event_flag_handlers, link) {
737 if (handler->conn_id == param) {
738 event_notifier_set(handler->notifier);
739 return 0;
740 }
741 }
742 return HV_STATUS_INVALID_CONNECTION_ID;
743 }
744
745 static HvSynDbgHandler hv_syndbg_handler;
746 static void *hv_syndbg_context;
747
748 void hyperv_set_syndbg_handler(HvSynDbgHandler handler, void *context)
749 {
750 assert(!hv_syndbg_handler);
751 hv_syndbg_handler = handler;
752 hv_syndbg_context = context;
753 }
754
755 uint16_t hyperv_hcall_reset_dbg_session(uint64_t outgpa)
756 {
757 uint16_t ret;
758 HvSynDbgMsg msg;
759 struct hyperv_reset_debug_session_output *reset_dbg_session = NULL;
760 hwaddr len;
761
762 if (!hv_syndbg_handler) {
763 ret = HV_STATUS_INVALID_HYPERCALL_CODE;
764 goto cleanup;
765 }
766
767 len = sizeof(*reset_dbg_session);
768 reset_dbg_session = physical_memory_map(outgpa, &len, 1);
769 if (!reset_dbg_session || len < sizeof(*reset_dbg_session)) {
770 ret = HV_STATUS_INSUFFICIENT_MEMORY;
771 goto cleanup;
772 }
773
774 msg.type = HV_SYNDBG_MSG_CONNECTION_INFO;
775 ret = hv_syndbg_handler(hv_syndbg_context, &msg);
776 if (ret) {
777 goto cleanup;
778 }
779
780 reset_dbg_session->host_ip = msg.u.connection_info.host_ip;
781 reset_dbg_session->host_port = msg.u.connection_info.host_port;
782 /* The following fields are only used as validation for KDVM */
783 memset(&reset_dbg_session->host_mac, 0,
784 sizeof(reset_dbg_session->host_mac));
785 reset_dbg_session->target_ip = msg.u.connection_info.host_ip;
786 reset_dbg_session->target_port = msg.u.connection_info.host_port;
787 memset(&reset_dbg_session->target_mac, 0,
788 sizeof(reset_dbg_session->target_mac));
789 cleanup:
790 if (reset_dbg_session) {
791 physical_memory_unmap(reset_dbg_session,
792 sizeof(*reset_dbg_session), 1, len);
793 }
794
795 return ret;
796 }
797
798 uint16_t hyperv_hcall_retreive_dbg_data(uint64_t ingpa, uint64_t outgpa,
799 bool fast)
800 {
801 uint16_t ret;
802 struct hyperv_retrieve_debug_data_input *debug_data_in = NULL;
803 struct hyperv_retrieve_debug_data_output *debug_data_out = NULL;
804 hwaddr in_len, out_len;
805 HvSynDbgMsg msg;
806
807 if (fast || !hv_syndbg_handler) {
808 ret = HV_STATUS_INVALID_HYPERCALL_CODE;
809 goto cleanup;
810 }
811
812 in_len = sizeof(*debug_data_in);
813 debug_data_in = physical_memory_map(ingpa, &in_len, 0);
814 if (!debug_data_in || in_len < sizeof(*debug_data_in)) {
815 ret = HV_STATUS_INSUFFICIENT_MEMORY;
816 goto cleanup;
817 }
818
819 out_len = sizeof(*debug_data_out);
820 debug_data_out = physical_memory_map(outgpa, &out_len, 1);
821 if (!debug_data_out || out_len < sizeof(*debug_data_out)) {
822 ret = HV_STATUS_INSUFFICIENT_MEMORY;
823 goto cleanup;
824 }
825
826 msg.type = HV_SYNDBG_MSG_RECV;
827 msg.u.recv.buf_gpa = outgpa + sizeof(*debug_data_out);
828 msg.u.recv.count = TARGET_PAGE_SIZE - sizeof(*debug_data_out);
829 msg.u.recv.options = debug_data_in->options;
830 msg.u.recv.timeout = debug_data_in->timeout;
831 msg.u.recv.is_raw = true;
832 ret = hv_syndbg_handler(hv_syndbg_context, &msg);
833 if (ret == HV_STATUS_NO_DATA) {
834 debug_data_out->retrieved_count = 0;
835 debug_data_out->remaining_count = debug_data_in->count;
836 goto cleanup;
837 } else if (ret != HV_STATUS_SUCCESS) {
838 goto cleanup;
839 }
840
841 debug_data_out->retrieved_count = msg.u.recv.retrieved_count;
842 debug_data_out->remaining_count =
843 debug_data_in->count - msg.u.recv.retrieved_count;
844 cleanup:
845 if (debug_data_out) {
846 physical_memory_unmap(debug_data_out, sizeof(*debug_data_out), 1,
847 out_len);
848 }
849
850 if (debug_data_in) {
851 physical_memory_unmap(debug_data_in, sizeof(*debug_data_in), 0,
852 in_len);
853 }
854
855 return ret;
856 }
857
858 uint16_t hyperv_hcall_post_dbg_data(uint64_t ingpa, uint64_t outgpa, bool fast)
859 {
860 uint16_t ret;
861 struct hyperv_post_debug_data_input *post_data_in = NULL;
862 struct hyperv_post_debug_data_output *post_data_out = NULL;
863 hwaddr in_len, out_len;
864 HvSynDbgMsg msg;
865
866 if (fast || !hv_syndbg_handler) {
867 ret = HV_STATUS_INVALID_HYPERCALL_CODE;
868 goto cleanup;
869 }
870
871 in_len = sizeof(*post_data_in);
872 post_data_in = physical_memory_map(ingpa, &in_len, 0);
873 if (!post_data_in || in_len < sizeof(*post_data_in)) {
874 ret = HV_STATUS_INSUFFICIENT_MEMORY;
875 goto cleanup;
876 }
877
878 if (post_data_in->count > TARGET_PAGE_SIZE - sizeof(*post_data_in)) {
879 ret = HV_STATUS_INVALID_PARAMETER;
880 goto cleanup;
881 }
882
883 out_len = sizeof(*post_data_out);
884 post_data_out = physical_memory_map(outgpa, &out_len, 1);
885 if (!post_data_out || out_len < sizeof(*post_data_out)) {
886 ret = HV_STATUS_INSUFFICIENT_MEMORY;
887 goto cleanup;
888 }
889
890 msg.type = HV_SYNDBG_MSG_SEND;
891 msg.u.send.buf_gpa = ingpa + sizeof(*post_data_in);
892 msg.u.send.count = post_data_in->count;
893 msg.u.send.is_raw = true;
894 ret = hv_syndbg_handler(hv_syndbg_context, &msg);
895 if (ret != HV_STATUS_SUCCESS) {
896 goto cleanup;
897 }
898
899 post_data_out->pending_count = msg.u.send.pending_count;
900 ret = post_data_out->pending_count ? HV_STATUS_INSUFFICIENT_BUFFERS :
901 HV_STATUS_SUCCESS;
902 cleanup:
903 if (post_data_out) {
904 physical_memory_unmap(post_data_out,
905 sizeof(*post_data_out), 1, out_len);
906 }
907
908 if (post_data_in) {
909 physical_memory_unmap(post_data_in,
910 sizeof(*post_data_in), 0, in_len);
911 }
912
913 return ret;
914 }
915
916 uint32_t hyperv_syndbg_send(uint64_t ingpa, uint32_t count)
917 {
918 HvSynDbgMsg msg;
919
920 if (!hv_syndbg_handler) {
921 return HV_SYNDBG_STATUS_INVALID;
922 }
923
924 msg.type = HV_SYNDBG_MSG_SEND;
925 msg.u.send.buf_gpa = ingpa;
926 msg.u.send.count = count;
927 msg.u.send.is_raw = false;
928 if (hv_syndbg_handler(hv_syndbg_context, &msg)) {
929 return HV_SYNDBG_STATUS_INVALID;
930 }
931
932 return HV_SYNDBG_STATUS_SEND_SUCCESS;
933 }
934
935 uint32_t hyperv_syndbg_recv(uint64_t ingpa, uint32_t count)
936 {
937 uint16_t ret;
938 HvSynDbgMsg msg;
939
940 if (!hv_syndbg_handler) {
941 return HV_SYNDBG_STATUS_INVALID;
942 }
943
944 msg.type = HV_SYNDBG_MSG_RECV;
945 msg.u.recv.buf_gpa = ingpa;
946 msg.u.recv.count = count;
947 msg.u.recv.options = 0;
948 msg.u.recv.timeout = 0;
949 msg.u.recv.is_raw = false;
950 ret = hv_syndbg_handler(hv_syndbg_context, &msg);
951 if (ret != HV_STATUS_SUCCESS) {
952 return 0;
953 }
954
955 return HV_SYNDBG_STATUS_SET_SIZE(HV_SYNDBG_STATUS_RECV_SUCCESS,
956 msg.u.recv.retrieved_count);
957 }
958
959 void hyperv_syndbg_set_pending_page(uint64_t ingpa)
960 {
961 HvSynDbgMsg msg;
962
963 if (!hv_syndbg_handler) {
964 return;
965 }
966
967 msg.type = HV_SYNDBG_MSG_SET_PENDING_PAGE;
968 msg.u.pending_page.buf_gpa = ingpa;
969 hv_syndbg_handler(hv_syndbg_context, &msg);
970 }
971
972 uint64_t hyperv_syndbg_query_options(void)
973 {
974 HvSynDbgMsg msg;
975
976 if (!hv_syndbg_handler) {
977 return 0;
978 }
979
980 msg.type = HV_SYNDBG_MSG_QUERY_OPTIONS;
981 if (hv_syndbg_handler(hv_syndbg_context, &msg) != HV_STATUS_SUCCESS) {
982 return 0;
983 }
984
985 return msg.u.query_options.options;
986 }
987
988 static bool vmbus_recommended_features_enabled;
989
990 bool hyperv_are_vmbus_recommended_features_enabled(void)
991 {
992 return vmbus_recommended_features_enabled;
993 }
994
995 void hyperv_set_vmbus_recommended_features_enabled(void)
996 {
997 vmbus_recommended_features_enabled = true;
998 }