Merge remote-tracking branch 'remotes/amarkovic/tags/mips-queue-oct-2018-part-2-v2...
[qemu.git] / hw / hyperv / hyperv.c
bloba28e7249d899025d31d58272b416e8350260e289
1 /*
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.
8 */
10 #include "qemu/osdep.h"
11 #include "qemu/main-loop.h"
12 #include "qapi/error.h"
13 #include "exec/address-spaces.h"
14 #include "sysemu/kvm.h"
15 #include "qemu/bitops.h"
16 #include "qemu/error-report.h"
17 #include "qemu/queue.h"
18 #include "qemu/rcu.h"
19 #include "qemu/rcu_queue.h"
20 #include "hw/hyperv/hyperv.h"
22 typedef struct SynICState {
23 DeviceState parent_obj;
25 CPUState *cs;
27 bool enabled;
28 hwaddr msg_page_addr;
29 hwaddr event_page_addr;
30 MemoryRegion msg_page_mr;
31 MemoryRegion event_page_mr;
32 struct hyperv_message_page *msg_page;
33 struct hyperv_event_flags_page *event_page;
34 } SynICState;
36 #define TYPE_SYNIC "hyperv-synic"
37 #define SYNIC(obj) OBJECT_CHECK(SynICState, (obj), TYPE_SYNIC)
39 static SynICState *get_synic(CPUState *cs)
41 return SYNIC(object_resolve_path_component(OBJECT(cs), "synic"));
44 static void synic_update(SynICState *synic, bool enable,
45 hwaddr msg_page_addr, hwaddr event_page_addr)
48 synic->enabled = enable;
49 if (synic->msg_page_addr != msg_page_addr) {
50 if (synic->msg_page_addr) {
51 memory_region_del_subregion(get_system_memory(),
52 &synic->msg_page_mr);
54 if (msg_page_addr) {
55 memory_region_add_subregion(get_system_memory(), msg_page_addr,
56 &synic->msg_page_mr);
58 synic->msg_page_addr = msg_page_addr;
60 if (synic->event_page_addr != event_page_addr) {
61 if (synic->event_page_addr) {
62 memory_region_del_subregion(get_system_memory(),
63 &synic->event_page_mr);
65 if (event_page_addr) {
66 memory_region_add_subregion(get_system_memory(), event_page_addr,
67 &synic->event_page_mr);
69 synic->event_page_addr = event_page_addr;
73 void hyperv_synic_update(CPUState *cs, bool enable,
74 hwaddr msg_page_addr, hwaddr event_page_addr)
76 SynICState *synic = get_synic(cs);
78 if (!synic) {
79 return;
82 synic_update(synic, enable, msg_page_addr, event_page_addr);
85 static void synic_realize(DeviceState *dev, Error **errp)
87 Object *obj = OBJECT(dev);
88 SynICState *synic = SYNIC(dev);
89 char *msgp_name, *eventp_name;
90 uint32_t vp_index;
92 /* memory region names have to be globally unique */
93 vp_index = hyperv_vp_index(synic->cs);
94 msgp_name = g_strdup_printf("synic-%u-msg-page", vp_index);
95 eventp_name = g_strdup_printf("synic-%u-event-page", vp_index);
97 memory_region_init_ram(&synic->msg_page_mr, obj, msgp_name,
98 sizeof(*synic->msg_page), &error_abort);
99 memory_region_init_ram(&synic->event_page_mr, obj, eventp_name,
100 sizeof(*synic->event_page), &error_abort);
101 synic->msg_page = memory_region_get_ram_ptr(&synic->msg_page_mr);
102 synic->event_page = memory_region_get_ram_ptr(&synic->event_page_mr);
104 g_free(msgp_name);
105 g_free(eventp_name);
107 static void synic_reset(DeviceState *dev)
109 SynICState *synic = SYNIC(dev);
110 memset(synic->msg_page, 0, sizeof(*synic->msg_page));
111 memset(synic->event_page, 0, sizeof(*synic->event_page));
112 synic_update(synic, false, 0, 0);
115 static void synic_class_init(ObjectClass *klass, void *data)
117 DeviceClass *dc = DEVICE_CLASS(klass);
119 dc->realize = synic_realize;
120 dc->reset = synic_reset;
121 dc->user_creatable = false;
124 void hyperv_synic_add(CPUState *cs)
126 Object *obj;
127 SynICState *synic;
129 obj = object_new(TYPE_SYNIC);
130 synic = SYNIC(obj);
131 synic->cs = cs;
132 object_property_add_child(OBJECT(cs), "synic", obj, &error_abort);
133 object_unref(obj);
134 object_property_set_bool(obj, true, "realized", &error_abort);
137 void hyperv_synic_reset(CPUState *cs)
139 device_reset(DEVICE(get_synic(cs)));
142 static const TypeInfo synic_type_info = {
143 .name = TYPE_SYNIC,
144 .parent = TYPE_DEVICE,
145 .instance_size = sizeof(SynICState),
146 .class_init = synic_class_init,
149 static void synic_register_types(void)
151 type_register_static(&synic_type_info);
154 type_init(synic_register_types)
157 * KVM has its own message producers (SynIC timers). To guarantee
158 * serialization with both KVM vcpu and the guest cpu, the messages are first
159 * staged in an intermediate area and then posted to the SynIC message page in
160 * the vcpu thread.
162 typedef struct HvSintStagedMessage {
163 /* message content staged by hyperv_post_msg */
164 struct hyperv_message msg;
165 /* callback + data (r/o) to complete the processing in a BH */
166 HvSintMsgCb cb;
167 void *cb_data;
168 /* message posting status filled by cpu_post_msg */
169 int status;
170 /* passing the buck: */
171 enum {
172 /* initial state */
173 HV_STAGED_MSG_FREE,
175 * hyperv_post_msg (e.g. in main loop) grabs the staged area (FREE ->
176 * BUSY), copies msg, and schedules cpu_post_msg on the assigned cpu
178 HV_STAGED_MSG_BUSY,
180 * cpu_post_msg (vcpu thread) tries to copy staged msg to msg slot,
181 * notify the guest, records the status, marks the posting done (BUSY
182 * -> POSTED), and schedules sint_msg_bh BH
184 HV_STAGED_MSG_POSTED,
186 * sint_msg_bh (BH) verifies that the posting is done, runs the
187 * callback, and starts over (POSTED -> FREE)
189 } state;
190 } HvSintStagedMessage;
192 struct HvSintRoute {
193 uint32_t sint;
194 SynICState *synic;
195 int gsi;
196 EventNotifier sint_set_notifier;
197 EventNotifier sint_ack_notifier;
199 HvSintStagedMessage *staged_msg;
201 unsigned refcount;
204 static CPUState *hyperv_find_vcpu(uint32_t vp_index)
206 CPUState *cs = qemu_get_cpu(vp_index);
207 assert(hyperv_vp_index(cs) == vp_index);
208 return cs;
212 * BH to complete the processing of a staged message.
214 static void sint_msg_bh(void *opaque)
216 HvSintRoute *sint_route = opaque;
217 HvSintStagedMessage *staged_msg = sint_route->staged_msg;
219 if (atomic_read(&staged_msg->state) != HV_STAGED_MSG_POSTED) {
220 /* status nor ready yet (spurious ack from guest?), ignore */
221 return;
224 staged_msg->cb(staged_msg->cb_data, staged_msg->status);
225 staged_msg->status = 0;
227 /* staged message processing finished, ready to start over */
228 atomic_set(&staged_msg->state, HV_STAGED_MSG_FREE);
229 /* drop the reference taken in hyperv_post_msg */
230 hyperv_sint_route_unref(sint_route);
234 * Worker to transfer the message from the staging area into the SynIC message
235 * page in vcpu context.
237 static void cpu_post_msg(CPUState *cs, run_on_cpu_data data)
239 HvSintRoute *sint_route = data.host_ptr;
240 HvSintStagedMessage *staged_msg = sint_route->staged_msg;
241 SynICState *synic = sint_route->synic;
242 struct hyperv_message *dst_msg;
243 bool wait_for_sint_ack = false;
245 assert(staged_msg->state == HV_STAGED_MSG_BUSY);
247 if (!synic->enabled || !synic->msg_page_addr) {
248 staged_msg->status = -ENXIO;
249 goto posted;
252 dst_msg = &synic->msg_page->slot[sint_route->sint];
254 if (dst_msg->header.message_type != HV_MESSAGE_NONE) {
255 dst_msg->header.message_flags |= HV_MESSAGE_FLAG_PENDING;
256 staged_msg->status = -EAGAIN;
257 wait_for_sint_ack = true;
258 } else {
259 memcpy(dst_msg, &staged_msg->msg, sizeof(*dst_msg));
260 staged_msg->status = hyperv_sint_route_set_sint(sint_route);
263 memory_region_set_dirty(&synic->msg_page_mr, 0, sizeof(*synic->msg_page));
265 posted:
266 atomic_set(&staged_msg->state, HV_STAGED_MSG_POSTED);
268 * Notify the msg originator of the progress made; if the slot was busy we
269 * set msg_pending flag in it so it will be the guest who will do EOM and
270 * trigger the notification from KVM via sint_ack_notifier
272 if (!wait_for_sint_ack) {
273 aio_bh_schedule_oneshot(qemu_get_aio_context(), sint_msg_bh,
274 sint_route);
279 * Post a Hyper-V message to the staging area, for delivery to guest in the
280 * vcpu thread.
282 int hyperv_post_msg(HvSintRoute *sint_route, struct hyperv_message *src_msg)
284 HvSintStagedMessage *staged_msg = sint_route->staged_msg;
286 assert(staged_msg);
288 /* grab the staging area */
289 if (atomic_cmpxchg(&staged_msg->state, HV_STAGED_MSG_FREE,
290 HV_STAGED_MSG_BUSY) != HV_STAGED_MSG_FREE) {
291 return -EAGAIN;
294 memcpy(&staged_msg->msg, src_msg, sizeof(*src_msg));
296 /* hold a reference on sint_route until the callback is finished */
297 hyperv_sint_route_ref(sint_route);
299 /* schedule message posting attempt in vcpu thread */
300 async_run_on_cpu(sint_route->synic->cs, cpu_post_msg,
301 RUN_ON_CPU_HOST_PTR(sint_route));
302 return 0;
305 static void sint_ack_handler(EventNotifier *notifier)
307 HvSintRoute *sint_route = container_of(notifier, HvSintRoute,
308 sint_ack_notifier);
309 event_notifier_test_and_clear(notifier);
312 * the guest consumed the previous message so complete the current one with
313 * -EAGAIN and let the msg originator retry
315 aio_bh_schedule_oneshot(qemu_get_aio_context(), sint_msg_bh, sint_route);
319 * Set given event flag for a given sint on a given vcpu, and signal the sint.
321 int hyperv_set_event_flag(HvSintRoute *sint_route, unsigned eventno)
323 int ret;
324 SynICState *synic = sint_route->synic;
325 unsigned long *flags, set_mask;
326 unsigned set_idx;
328 if (eventno > HV_EVENT_FLAGS_COUNT) {
329 return -EINVAL;
331 if (!synic->enabled || !synic->event_page_addr) {
332 return -ENXIO;
335 set_idx = BIT_WORD(eventno);
336 set_mask = BIT_MASK(eventno);
337 flags = synic->event_page->slot[sint_route->sint].flags;
339 if ((atomic_fetch_or(&flags[set_idx], set_mask) & set_mask) != set_mask) {
340 memory_region_set_dirty(&synic->event_page_mr, 0,
341 sizeof(*synic->event_page));
342 ret = hyperv_sint_route_set_sint(sint_route);
343 } else {
344 ret = 0;
346 return ret;
349 HvSintRoute *hyperv_sint_route_new(uint32_t vp_index, uint32_t sint,
350 HvSintMsgCb cb, void *cb_data)
352 HvSintRoute *sint_route;
353 EventNotifier *ack_notifier;
354 int r, gsi;
355 CPUState *cs;
356 SynICState *synic;
358 cs = hyperv_find_vcpu(vp_index);
359 if (!cs) {
360 return NULL;
363 synic = get_synic(cs);
364 if (!synic) {
365 return NULL;
368 sint_route = g_new0(HvSintRoute, 1);
369 r = event_notifier_init(&sint_route->sint_set_notifier, false);
370 if (r) {
371 goto err;
375 ack_notifier = cb ? &sint_route->sint_ack_notifier : NULL;
376 if (ack_notifier) {
377 sint_route->staged_msg = g_new0(HvSintStagedMessage, 1);
378 sint_route->staged_msg->cb = cb;
379 sint_route->staged_msg->cb_data = cb_data;
381 r = event_notifier_init(ack_notifier, false);
382 if (r) {
383 goto err_sint_set_notifier;
386 event_notifier_set_handler(ack_notifier, sint_ack_handler);
389 gsi = kvm_irqchip_add_hv_sint_route(kvm_state, vp_index, sint);
390 if (gsi < 0) {
391 goto err_gsi;
394 r = kvm_irqchip_add_irqfd_notifier_gsi(kvm_state,
395 &sint_route->sint_set_notifier,
396 ack_notifier, gsi);
397 if (r) {
398 goto err_irqfd;
400 sint_route->gsi = gsi;
401 sint_route->synic = synic;
402 sint_route->sint = sint;
403 sint_route->refcount = 1;
405 return sint_route;
407 err_irqfd:
408 kvm_irqchip_release_virq(kvm_state, gsi);
409 err_gsi:
410 if (ack_notifier) {
411 event_notifier_set_handler(ack_notifier, NULL);
412 event_notifier_cleanup(ack_notifier);
413 g_free(sint_route->staged_msg);
415 err_sint_set_notifier:
416 event_notifier_cleanup(&sint_route->sint_set_notifier);
417 err:
418 g_free(sint_route);
420 return NULL;
423 void hyperv_sint_route_ref(HvSintRoute *sint_route)
425 sint_route->refcount++;
428 void hyperv_sint_route_unref(HvSintRoute *sint_route)
430 if (!sint_route) {
431 return;
434 assert(sint_route->refcount > 0);
436 if (--sint_route->refcount) {
437 return;
440 kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state,
441 &sint_route->sint_set_notifier,
442 sint_route->gsi);
443 kvm_irqchip_release_virq(kvm_state, sint_route->gsi);
444 if (sint_route->staged_msg) {
445 event_notifier_set_handler(&sint_route->sint_ack_notifier, NULL);
446 event_notifier_cleanup(&sint_route->sint_ack_notifier);
447 g_free(sint_route->staged_msg);
449 event_notifier_cleanup(&sint_route->sint_set_notifier);
450 g_free(sint_route);
453 int hyperv_sint_route_set_sint(HvSintRoute *sint_route)
455 return event_notifier_set(&sint_route->sint_set_notifier);
458 typedef struct MsgHandler {
459 struct rcu_head rcu;
460 QLIST_ENTRY(MsgHandler) link;
461 uint32_t conn_id;
462 HvMsgHandler handler;
463 void *data;
464 } MsgHandler;
466 typedef struct EventFlagHandler {
467 struct rcu_head rcu;
468 QLIST_ENTRY(EventFlagHandler) link;
469 uint32_t conn_id;
470 EventNotifier *notifier;
471 } EventFlagHandler;
473 static QLIST_HEAD(, MsgHandler) msg_handlers;
474 static QLIST_HEAD(, EventFlagHandler) event_flag_handlers;
475 static QemuMutex handlers_mutex;
477 static void __attribute__((constructor)) hv_init(void)
479 QLIST_INIT(&msg_handlers);
480 QLIST_INIT(&event_flag_handlers);
481 qemu_mutex_init(&handlers_mutex);
484 int hyperv_set_msg_handler(uint32_t conn_id, HvMsgHandler handler, void *data)
486 int ret;
487 MsgHandler *mh;
489 qemu_mutex_lock(&handlers_mutex);
490 QLIST_FOREACH(mh, &msg_handlers, link) {
491 if (mh->conn_id == conn_id) {
492 if (handler) {
493 ret = -EEXIST;
494 } else {
495 QLIST_REMOVE_RCU(mh, link);
496 g_free_rcu(mh, rcu);
497 ret = 0;
499 goto unlock;
503 if (handler) {
504 mh = g_new(MsgHandler, 1);
505 mh->conn_id = conn_id;
506 mh->handler = handler;
507 mh->data = data;
508 QLIST_INSERT_HEAD_RCU(&msg_handlers, mh, link);
509 ret = 0;
510 } else {
511 ret = -ENOENT;
513 unlock:
514 qemu_mutex_unlock(&handlers_mutex);
515 return ret;
518 uint16_t hyperv_hcall_post_message(uint64_t param, bool fast)
520 uint16_t ret;
521 hwaddr len;
522 struct hyperv_post_message_input *msg;
523 MsgHandler *mh;
525 if (fast) {
526 return HV_STATUS_INVALID_HYPERCALL_CODE;
528 if (param & (__alignof__(*msg) - 1)) {
529 return HV_STATUS_INVALID_ALIGNMENT;
532 len = sizeof(*msg);
533 msg = cpu_physical_memory_map(param, &len, 0);
534 if (len < sizeof(*msg)) {
535 ret = HV_STATUS_INSUFFICIENT_MEMORY;
536 goto unmap;
538 if (msg->payload_size > sizeof(msg->payload)) {
539 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
540 goto unmap;
543 ret = HV_STATUS_INVALID_CONNECTION_ID;
544 rcu_read_lock();
545 QLIST_FOREACH_RCU(mh, &msg_handlers, link) {
546 if (mh->conn_id == (msg->connection_id & HV_CONNECTION_ID_MASK)) {
547 ret = mh->handler(msg, mh->data);
548 break;
551 rcu_read_unlock();
553 unmap:
554 cpu_physical_memory_unmap(msg, len, 0, 0);
555 return ret;
558 static int set_event_flag_handler(uint32_t conn_id, EventNotifier *notifier)
560 int ret;
561 EventFlagHandler *handler;
563 qemu_mutex_lock(&handlers_mutex);
564 QLIST_FOREACH(handler, &event_flag_handlers, link) {
565 if (handler->conn_id == conn_id) {
566 if (notifier) {
567 ret = -EEXIST;
568 } else {
569 QLIST_REMOVE_RCU(handler, link);
570 g_free_rcu(handler, rcu);
571 ret = 0;
573 goto unlock;
577 if (notifier) {
578 handler = g_new(EventFlagHandler, 1);
579 handler->conn_id = conn_id;
580 handler->notifier = notifier;
581 QLIST_INSERT_HEAD_RCU(&event_flag_handlers, handler, link);
582 ret = 0;
583 } else {
584 ret = -ENOENT;
586 unlock:
587 qemu_mutex_unlock(&handlers_mutex);
588 return ret;
591 static bool process_event_flags_userspace;
593 int hyperv_set_event_flag_handler(uint32_t conn_id, EventNotifier *notifier)
595 if (!process_event_flags_userspace &&
596 !kvm_check_extension(kvm_state, KVM_CAP_HYPERV_EVENTFD)) {
597 process_event_flags_userspace = true;
599 warn_report("Hyper-V event signaling is not supported by this kernel; "
600 "using slower userspace hypercall processing");
603 if (!process_event_flags_userspace) {
604 struct kvm_hyperv_eventfd hvevfd = {
605 .conn_id = conn_id,
606 .fd = notifier ? event_notifier_get_fd(notifier) : -1,
607 .flags = notifier ? 0 : KVM_HYPERV_EVENTFD_DEASSIGN,
610 return kvm_vm_ioctl(kvm_state, KVM_HYPERV_EVENTFD, &hvevfd);
612 return set_event_flag_handler(conn_id, notifier);
615 uint16_t hyperv_hcall_signal_event(uint64_t param, bool fast)
617 uint16_t ret;
618 EventFlagHandler *handler;
620 if (unlikely(!fast)) {
621 hwaddr addr = param;
623 if (addr & (__alignof__(addr) - 1)) {
624 return HV_STATUS_INVALID_ALIGNMENT;
627 param = ldq_phys(&address_space_memory, addr);
631 * Per spec, bits 32-47 contain the extra "flag number". However, we
632 * have no use for it, and in all known usecases it is zero, so just
633 * report lookup failure if it isn't.
635 if (param & 0xffff00000000ULL) {
636 return HV_STATUS_INVALID_PORT_ID;
638 /* remaining bits are reserved-zero */
639 if (param & ~HV_CONNECTION_ID_MASK) {
640 return HV_STATUS_INVALID_HYPERCALL_INPUT;
643 ret = HV_STATUS_INVALID_CONNECTION_ID;
644 rcu_read_lock();
645 QLIST_FOREACH_RCU(handler, &event_flag_handlers, link) {
646 if (handler->conn_id == param) {
647 event_notifier_set(handler->notifier);
648 ret = 0;
649 break;
652 rcu_read_unlock();
653 return ret;