2 * Hyper-V guest/hypervisor interaction
4 * Copyright (c) 2015-2018 Virtuozzo International GmbH.
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.
10 #include "qemu/osdep.h"
11 #include "qemu/main-loop.h"
12 #include "qemu/module.h"
13 #include "qapi/error.h"
14 #include "exec/address-spaces.h"
15 #include "sysemu/kvm.h"
16 #include "qemu/bitops.h"
17 #include "qemu/error-report.h"
18 #include "qemu/lockable.h"
19 #include "qemu/queue.h"
21 #include "qemu/rcu_queue.h"
22 #include "hw/hyperv/hyperv.h"
24 typedef struct SynICState
{
25 DeviceState parent_obj
;
31 hwaddr event_page_addr
;
32 MemoryRegion msg_page_mr
;
33 MemoryRegion event_page_mr
;
34 struct hyperv_message_page
*msg_page
;
35 struct hyperv_event_flags_page
*event_page
;
38 #define TYPE_SYNIC "hyperv-synic"
39 #define SYNIC(obj) OBJECT_CHECK(SynICState, (obj), TYPE_SYNIC)
41 static bool synic_enabled
;
43 bool hyperv_is_synic_enabled(void)
48 static SynICState
*get_synic(CPUState
*cs
)
50 return SYNIC(object_resolve_path_component(OBJECT(cs
), "synic"));
53 static void synic_update(SynICState
*synic
, bool enable
,
54 hwaddr msg_page_addr
, hwaddr event_page_addr
)
57 synic
->enabled
= enable
;
58 if (synic
->msg_page_addr
!= msg_page_addr
) {
59 if (synic
->msg_page_addr
) {
60 memory_region_del_subregion(get_system_memory(),
64 memory_region_add_subregion(get_system_memory(), msg_page_addr
,
67 synic
->msg_page_addr
= msg_page_addr
;
69 if (synic
->event_page_addr
!= event_page_addr
) {
70 if (synic
->event_page_addr
) {
71 memory_region_del_subregion(get_system_memory(),
72 &synic
->event_page_mr
);
74 if (event_page_addr
) {
75 memory_region_add_subregion(get_system_memory(), event_page_addr
,
76 &synic
->event_page_mr
);
78 synic
->event_page_addr
= event_page_addr
;
82 void hyperv_synic_update(CPUState
*cs
, bool enable
,
83 hwaddr msg_page_addr
, hwaddr event_page_addr
)
85 SynICState
*synic
= get_synic(cs
);
91 synic_update(synic
, enable
, msg_page_addr
, event_page_addr
);
94 static void synic_realize(DeviceState
*dev
, Error
**errp
)
96 Object
*obj
= OBJECT(dev
);
97 SynICState
*synic
= SYNIC(dev
);
98 char *msgp_name
, *eventp_name
;
101 /* memory region names have to be globally unique */
102 vp_index
= hyperv_vp_index(synic
->cs
);
103 msgp_name
= g_strdup_printf("synic-%u-msg-page", vp_index
);
104 eventp_name
= g_strdup_printf("synic-%u-event-page", vp_index
);
106 memory_region_init_ram(&synic
->msg_page_mr
, obj
, msgp_name
,
107 sizeof(*synic
->msg_page
), &error_abort
);
108 memory_region_init_ram(&synic
->event_page_mr
, obj
, eventp_name
,
109 sizeof(*synic
->event_page
), &error_abort
);
110 synic
->msg_page
= memory_region_get_ram_ptr(&synic
->msg_page_mr
);
111 synic
->event_page
= memory_region_get_ram_ptr(&synic
->event_page_mr
);
116 static void synic_reset(DeviceState
*dev
)
118 SynICState
*synic
= SYNIC(dev
);
119 memset(synic
->msg_page
, 0, sizeof(*synic
->msg_page
));
120 memset(synic
->event_page
, 0, sizeof(*synic
->event_page
));
121 synic_update(synic
, false, 0, 0);
124 static void synic_class_init(ObjectClass
*klass
, void *data
)
126 DeviceClass
*dc
= DEVICE_CLASS(klass
);
128 dc
->realize
= synic_realize
;
129 dc
->reset
= synic_reset
;
130 dc
->user_creatable
= false;
133 void hyperv_synic_add(CPUState
*cs
)
138 obj
= object_new(TYPE_SYNIC
);
141 object_property_add_child(OBJECT(cs
), "synic", obj
);
143 qdev_realize(DEVICE(obj
), NULL
, &error_abort
);
144 synic_enabled
= true;
147 void hyperv_synic_reset(CPUState
*cs
)
149 SynICState
*synic
= get_synic(cs
);
152 device_legacy_reset(DEVICE(synic
));
156 static const TypeInfo synic_type_info
= {
158 .parent
= TYPE_DEVICE
,
159 .instance_size
= sizeof(SynICState
),
160 .class_init
= synic_class_init
,
163 static void synic_register_types(void)
165 type_register_static(&synic_type_info
);
168 type_init(synic_register_types
)
171 * KVM has its own message producers (SynIC timers). To guarantee
172 * serialization with both KVM vcpu and the guest cpu, the messages are first
173 * staged in an intermediate area and then posted to the SynIC message page in
176 typedef struct HvSintStagedMessage
{
177 /* message content staged by hyperv_post_msg */
178 struct hyperv_message msg
;
179 /* callback + data (r/o) to complete the processing in a BH */
182 /* message posting status filled by cpu_post_msg */
184 /* passing the buck: */
189 * hyperv_post_msg (e.g. in main loop) grabs the staged area (FREE ->
190 * BUSY), copies msg, and schedules cpu_post_msg on the assigned cpu
194 * cpu_post_msg (vcpu thread) tries to copy staged msg to msg slot,
195 * notify the guest, records the status, marks the posting done (BUSY
196 * -> POSTED), and schedules sint_msg_bh BH
198 HV_STAGED_MSG_POSTED
,
200 * sint_msg_bh (BH) verifies that the posting is done, runs the
201 * callback, and starts over (POSTED -> FREE)
204 } HvSintStagedMessage
;
210 EventNotifier sint_set_notifier
;
211 EventNotifier sint_ack_notifier
;
213 HvSintStagedMessage
*staged_msg
;
218 static CPUState
*hyperv_find_vcpu(uint32_t vp_index
)
220 CPUState
*cs
= qemu_get_cpu(vp_index
);
221 assert(hyperv_vp_index(cs
) == vp_index
);
226 * BH to complete the processing of a staged message.
228 static void sint_msg_bh(void *opaque
)
230 HvSintRoute
*sint_route
= opaque
;
231 HvSintStagedMessage
*staged_msg
= sint_route
->staged_msg
;
233 if (atomic_read(&staged_msg
->state
) != HV_STAGED_MSG_POSTED
) {
234 /* status nor ready yet (spurious ack from guest?), ignore */
238 staged_msg
->cb(staged_msg
->cb_data
, staged_msg
->status
);
239 staged_msg
->status
= 0;
241 /* staged message processing finished, ready to start over */
242 atomic_set(&staged_msg
->state
, HV_STAGED_MSG_FREE
);
243 /* drop the reference taken in hyperv_post_msg */
244 hyperv_sint_route_unref(sint_route
);
248 * Worker to transfer the message from the staging area into the SynIC message
249 * page in vcpu context.
251 static void cpu_post_msg(CPUState
*cs
, run_on_cpu_data data
)
253 HvSintRoute
*sint_route
= data
.host_ptr
;
254 HvSintStagedMessage
*staged_msg
= sint_route
->staged_msg
;
255 SynICState
*synic
= sint_route
->synic
;
256 struct hyperv_message
*dst_msg
;
257 bool wait_for_sint_ack
= false;
259 assert(staged_msg
->state
== HV_STAGED_MSG_BUSY
);
261 if (!synic
->enabled
|| !synic
->msg_page_addr
) {
262 staged_msg
->status
= -ENXIO
;
266 dst_msg
= &synic
->msg_page
->slot
[sint_route
->sint
];
268 if (dst_msg
->header
.message_type
!= HV_MESSAGE_NONE
) {
269 dst_msg
->header
.message_flags
|= HV_MESSAGE_FLAG_PENDING
;
270 staged_msg
->status
= -EAGAIN
;
271 wait_for_sint_ack
= true;
273 memcpy(dst_msg
, &staged_msg
->msg
, sizeof(*dst_msg
));
274 staged_msg
->status
= hyperv_sint_route_set_sint(sint_route
);
277 memory_region_set_dirty(&synic
->msg_page_mr
, 0, sizeof(*synic
->msg_page
));
280 atomic_set(&staged_msg
->state
, HV_STAGED_MSG_POSTED
);
282 * Notify the msg originator of the progress made; if the slot was busy we
283 * set msg_pending flag in it so it will be the guest who will do EOM and
284 * trigger the notification from KVM via sint_ack_notifier
286 if (!wait_for_sint_ack
) {
287 aio_bh_schedule_oneshot(qemu_get_aio_context(), sint_msg_bh
,
293 * Post a Hyper-V message to the staging area, for delivery to guest in the
296 int hyperv_post_msg(HvSintRoute
*sint_route
, struct hyperv_message
*src_msg
)
298 HvSintStagedMessage
*staged_msg
= sint_route
->staged_msg
;
302 /* grab the staging area */
303 if (atomic_cmpxchg(&staged_msg
->state
, HV_STAGED_MSG_FREE
,
304 HV_STAGED_MSG_BUSY
) != HV_STAGED_MSG_FREE
) {
308 memcpy(&staged_msg
->msg
, src_msg
, sizeof(*src_msg
));
310 /* hold a reference on sint_route until the callback is finished */
311 hyperv_sint_route_ref(sint_route
);
313 /* schedule message posting attempt in vcpu thread */
314 async_run_on_cpu(sint_route
->synic
->cs
, cpu_post_msg
,
315 RUN_ON_CPU_HOST_PTR(sint_route
));
319 static void sint_ack_handler(EventNotifier
*notifier
)
321 HvSintRoute
*sint_route
= container_of(notifier
, HvSintRoute
,
323 event_notifier_test_and_clear(notifier
);
326 * the guest consumed the previous message so complete the current one with
327 * -EAGAIN and let the msg originator retry
329 aio_bh_schedule_oneshot(qemu_get_aio_context(), sint_msg_bh
, sint_route
);
333 * Set given event flag for a given sint on a given vcpu, and signal the sint.
335 int hyperv_set_event_flag(HvSintRoute
*sint_route
, unsigned eventno
)
338 SynICState
*synic
= sint_route
->synic
;
339 unsigned long *flags
, set_mask
;
342 if (eventno
> HV_EVENT_FLAGS_COUNT
) {
345 if (!synic
->enabled
|| !synic
->event_page_addr
) {
349 set_idx
= BIT_WORD(eventno
);
350 set_mask
= BIT_MASK(eventno
);
351 flags
= synic
->event_page
->slot
[sint_route
->sint
].flags
;
353 if ((atomic_fetch_or(&flags
[set_idx
], set_mask
) & set_mask
) != set_mask
) {
354 memory_region_set_dirty(&synic
->event_page_mr
, 0,
355 sizeof(*synic
->event_page
));
356 ret
= hyperv_sint_route_set_sint(sint_route
);
363 HvSintRoute
*hyperv_sint_route_new(uint32_t vp_index
, uint32_t sint
,
364 HvSintMsgCb cb
, void *cb_data
)
366 HvSintRoute
*sint_route
;
367 EventNotifier
*ack_notifier
;
372 cs
= hyperv_find_vcpu(vp_index
);
377 synic
= get_synic(cs
);
382 sint_route
= g_new0(HvSintRoute
, 1);
383 r
= event_notifier_init(&sint_route
->sint_set_notifier
, false);
389 ack_notifier
= cb
? &sint_route
->sint_ack_notifier
: NULL
;
391 sint_route
->staged_msg
= g_new0(HvSintStagedMessage
, 1);
392 sint_route
->staged_msg
->cb
= cb
;
393 sint_route
->staged_msg
->cb_data
= cb_data
;
395 r
= event_notifier_init(ack_notifier
, false);
397 goto err_sint_set_notifier
;
400 event_notifier_set_handler(ack_notifier
, sint_ack_handler
);
403 gsi
= kvm_irqchip_add_hv_sint_route(kvm_state
, vp_index
, sint
);
408 r
= kvm_irqchip_add_irqfd_notifier_gsi(kvm_state
,
409 &sint_route
->sint_set_notifier
,
414 sint_route
->gsi
= gsi
;
415 sint_route
->synic
= synic
;
416 sint_route
->sint
= sint
;
417 sint_route
->refcount
= 1;
422 kvm_irqchip_release_virq(kvm_state
, gsi
);
425 event_notifier_set_handler(ack_notifier
, NULL
);
426 event_notifier_cleanup(ack_notifier
);
427 g_free(sint_route
->staged_msg
);
429 err_sint_set_notifier
:
430 event_notifier_cleanup(&sint_route
->sint_set_notifier
);
437 void hyperv_sint_route_ref(HvSintRoute
*sint_route
)
439 sint_route
->refcount
++;
442 void hyperv_sint_route_unref(HvSintRoute
*sint_route
)
448 assert(sint_route
->refcount
> 0);
450 if (--sint_route
->refcount
) {
454 kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state
,
455 &sint_route
->sint_set_notifier
,
457 kvm_irqchip_release_virq(kvm_state
, sint_route
->gsi
);
458 if (sint_route
->staged_msg
) {
459 event_notifier_set_handler(&sint_route
->sint_ack_notifier
, NULL
);
460 event_notifier_cleanup(&sint_route
->sint_ack_notifier
);
461 g_free(sint_route
->staged_msg
);
463 event_notifier_cleanup(&sint_route
->sint_set_notifier
);
467 int hyperv_sint_route_set_sint(HvSintRoute
*sint_route
)
469 return event_notifier_set(&sint_route
->sint_set_notifier
);
472 typedef struct MsgHandler
{
474 QLIST_ENTRY(MsgHandler
) link
;
476 HvMsgHandler handler
;
480 typedef struct EventFlagHandler
{
482 QLIST_ENTRY(EventFlagHandler
) link
;
484 EventNotifier
*notifier
;
487 static QLIST_HEAD(, MsgHandler
) msg_handlers
;
488 static QLIST_HEAD(, EventFlagHandler
) event_flag_handlers
;
489 static QemuMutex handlers_mutex
;
491 static void __attribute__((constructor
)) hv_init(void)
493 QLIST_INIT(&msg_handlers
);
494 QLIST_INIT(&event_flag_handlers
);
495 qemu_mutex_init(&handlers_mutex
);
498 int hyperv_set_msg_handler(uint32_t conn_id
, HvMsgHandler handler
, void *data
)
503 QEMU_LOCK_GUARD(&handlers_mutex
);
504 QLIST_FOREACH(mh
, &msg_handlers
, link
) {
505 if (mh
->conn_id
== conn_id
) {
509 QLIST_REMOVE_RCU(mh
, link
);
518 mh
= g_new(MsgHandler
, 1);
519 mh
->conn_id
= conn_id
;
520 mh
->handler
= handler
;
522 QLIST_INSERT_HEAD_RCU(&msg_handlers
, mh
, link
);
531 uint16_t hyperv_hcall_post_message(uint64_t param
, bool fast
)
535 struct hyperv_post_message_input
*msg
;
539 return HV_STATUS_INVALID_HYPERCALL_CODE
;
541 if (param
& (__alignof__(*msg
) - 1)) {
542 return HV_STATUS_INVALID_ALIGNMENT
;
546 msg
= cpu_physical_memory_map(param
, &len
, 0);
547 if (len
< sizeof(*msg
)) {
548 ret
= HV_STATUS_INSUFFICIENT_MEMORY
;
551 if (msg
->payload_size
> sizeof(msg
->payload
)) {
552 ret
= HV_STATUS_INVALID_HYPERCALL_INPUT
;
556 ret
= HV_STATUS_INVALID_CONNECTION_ID
;
557 WITH_RCU_READ_LOCK_GUARD() {
558 QLIST_FOREACH_RCU(mh
, &msg_handlers
, link
) {
559 if (mh
->conn_id
== (msg
->connection_id
& HV_CONNECTION_ID_MASK
)) {
560 ret
= mh
->handler(msg
, mh
->data
);
567 cpu_physical_memory_unmap(msg
, len
, 0, 0);
571 static int set_event_flag_handler(uint32_t conn_id
, EventNotifier
*notifier
)
574 EventFlagHandler
*handler
;
576 QEMU_LOCK_GUARD(&handlers_mutex
);
577 QLIST_FOREACH(handler
, &event_flag_handlers
, link
) {
578 if (handler
->conn_id
== conn_id
) {
582 QLIST_REMOVE_RCU(handler
, link
);
583 g_free_rcu(handler
, rcu
);
591 handler
= g_new(EventFlagHandler
, 1);
592 handler
->conn_id
= conn_id
;
593 handler
->notifier
= notifier
;
594 QLIST_INSERT_HEAD_RCU(&event_flag_handlers
, handler
, link
);
603 static bool process_event_flags_userspace
;
605 int hyperv_set_event_flag_handler(uint32_t conn_id
, EventNotifier
*notifier
)
607 if (!process_event_flags_userspace
&&
608 !kvm_check_extension(kvm_state
, KVM_CAP_HYPERV_EVENTFD
)) {
609 process_event_flags_userspace
= true;
611 warn_report("Hyper-V event signaling is not supported by this kernel; "
612 "using slower userspace hypercall processing");
615 if (!process_event_flags_userspace
) {
616 struct kvm_hyperv_eventfd hvevfd
= {
618 .fd
= notifier
? event_notifier_get_fd(notifier
) : -1,
619 .flags
= notifier
? 0 : KVM_HYPERV_EVENTFD_DEASSIGN
,
622 return kvm_vm_ioctl(kvm_state
, KVM_HYPERV_EVENTFD
, &hvevfd
);
624 return set_event_flag_handler(conn_id
, notifier
);
627 uint16_t hyperv_hcall_signal_event(uint64_t param
, bool fast
)
629 EventFlagHandler
*handler
;
631 if (unlikely(!fast
)) {
634 if (addr
& (__alignof__(addr
) - 1)) {
635 return HV_STATUS_INVALID_ALIGNMENT
;
638 param
= ldq_phys(&address_space_memory
, addr
);
642 * Per spec, bits 32-47 contain the extra "flag number". However, we
643 * have no use for it, and in all known usecases it is zero, so just
644 * report lookup failure if it isn't.
646 if (param
& 0xffff00000000ULL
) {
647 return HV_STATUS_INVALID_PORT_ID
;
649 /* remaining bits are reserved-zero */
650 if (param
& ~HV_CONNECTION_ID_MASK
) {
651 return HV_STATUS_INVALID_HYPERCALL_INPUT
;
654 RCU_READ_LOCK_GUARD();
655 QLIST_FOREACH_RCU(handler
, &event_flag_handlers
, link
) {
656 if (handler
->conn_id
== param
) {
657 event_notifier_set(handler
->notifier
);
661 return HV_STATUS_INVALID_CONNECTION_ID
;