2 * KVM Microsoft Hyper-V emulation
4 * derived from arch/x86/kvm/x86.c
6 * Copyright (C) 2006 Qumranet, Inc.
7 * Copyright (C) 2008 Qumranet, Inc.
8 * Copyright IBM Corporation, 2008
9 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
19 * This work is licensed under the terms of the GNU GPL, version 2. See
20 * the COPYING file in the top-level directory.
29 #include <linux/kvm_host.h>
30 #include <linux/highmem.h>
31 #include <linux/sched/cputime.h>
32 #include <linux/eventfd.h>
34 #include <asm/apicdef.h>
35 #include <trace/events/kvm.h>
39 static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
41 return atomic64_read(&synic->sint[sint]);
44 static inline int synic_get_sint_vector(u64 sint_value)
46 if (sint_value & HV_SYNIC_SINT_MASKED)
48 return sint_value & HV_SYNIC_SINT_VECTOR_MASK;
51 static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic,
56 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
57 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
63 static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic,
69 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
70 sint_value = synic_read_sint(synic, i);
71 if (synic_get_sint_vector(sint_value) == vector &&
72 sint_value & HV_SYNIC_SINT_AUTO_EOI)
78 static void synic_update_vector(struct kvm_vcpu_hv_synic *synic,
81 if (vector < HV_SYNIC_FIRST_VALID_VECTOR)
84 if (synic_has_vector_connected(synic, vector))
85 __set_bit(vector, synic->vec_bitmap);
87 __clear_bit(vector, synic->vec_bitmap);
89 if (synic_has_vector_auto_eoi(synic, vector))
90 __set_bit(vector, synic->auto_eoi_bitmap);
92 __clear_bit(vector, synic->auto_eoi_bitmap);
95 static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint,
98 int vector, old_vector;
101 vector = data & HV_SYNIC_SINT_VECTOR_MASK;
102 masked = data & HV_SYNIC_SINT_MASKED;
105 * Valid vectors are 16-255, however, nested Hyper-V attempts to write
106 * default '0x10000' value on boot and this should not #GP. We need to
107 * allow zero-initing the register from host as well.
109 if (vector < HV_SYNIC_FIRST_VALID_VECTOR && !host && !masked)
112 * Guest may configure multiple SINTs to use the same vector, so
113 * we maintain a bitmap of vectors handled by synic, and a
114 * bitmap of vectors with auto-eoi behavior. The bitmaps are
115 * updated here, and atomically queried on fast paths.
117 old_vector = synic_read_sint(synic, sint) & HV_SYNIC_SINT_VECTOR_MASK;
119 atomic64_set(&synic->sint[sint], data);
121 synic_update_vector(synic, old_vector);
123 synic_update_vector(synic, vector);
125 /* Load SynIC vectors into EOI exit bitmap */
126 kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic));
130 static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx)
132 struct kvm_vcpu *vcpu = NULL;
135 if (vpidx < KVM_MAX_VCPUS)
136 vcpu = kvm_get_vcpu(kvm, vpidx);
137 if (vcpu && vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx)
139 kvm_for_each_vcpu(i, vcpu, kvm)
140 if (vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx)
145 static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vpidx)
147 struct kvm_vcpu *vcpu;
148 struct kvm_vcpu_hv_synic *synic;
150 vcpu = get_vcpu_by_vpidx(kvm, vpidx);
153 synic = vcpu_to_synic(vcpu);
154 return (synic->active) ? synic : NULL;
157 static void synic_clear_sint_msg_pending(struct kvm_vcpu_hv_synic *synic,
160 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
163 struct hv_message *msg;
164 struct hv_message_page *msg_page;
166 gpa = synic->msg_page & PAGE_MASK;
167 page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
168 if (is_error_page(page)) {
169 vcpu_err(vcpu, "Hyper-V SynIC can't get msg page, gpa 0x%llx\n",
173 msg_page = kmap_atomic(page);
175 msg = &msg_page->sint_message[sint];
176 msg->header.message_flags.msg_pending = 0;
178 kunmap_atomic(msg_page);
179 kvm_release_page_dirty(page);
180 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
183 static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
185 struct kvm *kvm = vcpu->kvm;
186 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
187 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
188 struct kvm_vcpu_hv_stimer *stimer;
189 int gsi, idx, stimers_pending;
191 trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint);
193 if (synic->msg_page & HV_SYNIC_SIMP_ENABLE)
194 synic_clear_sint_msg_pending(synic, sint);
196 /* Try to deliver pending Hyper-V SynIC timers messages */
198 for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) {
199 stimer = &hv_vcpu->stimer[idx];
200 if (stimer->msg_pending &&
201 (stimer->config & HV_STIMER_ENABLE) &&
202 HV_STIMER_SINT(stimer->config) == sint) {
203 set_bit(stimer->index,
204 hv_vcpu->stimer_pending_bitmap);
209 kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
211 idx = srcu_read_lock(&kvm->irq_srcu);
212 gsi = atomic_read(&synic->sint_to_gsi[sint]);
214 kvm_notify_acked_gsi(kvm, gsi);
215 srcu_read_unlock(&kvm->irq_srcu, idx);
218 static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
220 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
221 struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
223 hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
224 hv_vcpu->exit.u.synic.msr = msr;
225 hv_vcpu->exit.u.synic.control = synic->control;
226 hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
227 hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
229 kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
232 static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
233 u32 msr, u64 data, bool host)
235 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
241 trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host);
245 case HV_X64_MSR_SCONTROL:
246 synic->control = data;
248 synic_exit(synic, msr);
250 case HV_X64_MSR_SVERSION:
255 synic->version = data;
257 case HV_X64_MSR_SIEFP:
258 if ((data & HV_SYNIC_SIEFP_ENABLE) && !host &&
259 !synic->dont_zero_synic_pages)
260 if (kvm_clear_guest(vcpu->kvm,
261 data & PAGE_MASK, PAGE_SIZE)) {
265 synic->evt_page = data;
267 synic_exit(synic, msr);
269 case HV_X64_MSR_SIMP:
270 if ((data & HV_SYNIC_SIMP_ENABLE) && !host &&
271 !synic->dont_zero_synic_pages)
272 if (kvm_clear_guest(vcpu->kvm,
273 data & PAGE_MASK, PAGE_SIZE)) {
277 synic->msg_page = data;
279 synic_exit(synic, msr);
281 case HV_X64_MSR_EOM: {
284 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
285 kvm_hv_notify_acked_sint(vcpu, i);
288 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
289 ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host);
298 static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata)
307 case HV_X64_MSR_SCONTROL:
308 *pdata = synic->control;
310 case HV_X64_MSR_SVERSION:
311 *pdata = synic->version;
313 case HV_X64_MSR_SIEFP:
314 *pdata = synic->evt_page;
316 case HV_X64_MSR_SIMP:
317 *pdata = synic->msg_page;
322 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
323 *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
332 static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
334 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
335 struct kvm_lapic_irq irq;
338 if (sint >= ARRAY_SIZE(synic->sint))
341 vector = synic_get_sint_vector(synic_read_sint(synic, sint));
345 memset(&irq, 0, sizeof(irq));
346 irq.shorthand = APIC_DEST_SELF;
347 irq.dest_mode = APIC_DEST_PHYSICAL;
348 irq.delivery_mode = APIC_DM_FIXED;
352 ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq, NULL);
353 trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret);
357 int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vpidx, u32 sint)
359 struct kvm_vcpu_hv_synic *synic;
361 synic = synic_get(kvm, vpidx);
365 return synic_set_irq(synic, sint);
368 void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
370 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
373 trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector);
375 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
376 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
377 kvm_hv_notify_acked_sint(vcpu, i);
380 static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vpidx, u32 sint, int gsi)
382 struct kvm_vcpu_hv_synic *synic;
384 synic = synic_get(kvm, vpidx);
388 if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
391 atomic_set(&synic->sint_to_gsi[sint], gsi);
395 void kvm_hv_irq_routing_update(struct kvm *kvm)
397 struct kvm_irq_routing_table *irq_rt;
398 struct kvm_kernel_irq_routing_entry *e;
401 irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
402 lockdep_is_held(&kvm->irq_lock));
404 for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
405 hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
406 if (e->type == KVM_IRQ_ROUTING_HV_SINT)
407 kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
408 e->hv_sint.sint, gsi);
413 static void synic_init(struct kvm_vcpu_hv_synic *synic)
417 memset(synic, 0, sizeof(*synic));
418 synic->version = HV_SYNIC_VERSION_1;
419 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
420 atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
421 atomic_set(&synic->sint_to_gsi[i], -1);
425 static u64 get_time_ref_counter(struct kvm *kvm)
427 struct kvm_hv *hv = &kvm->arch.hyperv;
428 struct kvm_vcpu *vcpu;
432 * The guest has not set up the TSC page or the clock isn't
433 * stable, fall back to get_kvmclock_ns.
435 if (!hv->tsc_ref.tsc_sequence)
436 return div_u64(get_kvmclock_ns(kvm), 100);
438 vcpu = kvm_get_vcpu(kvm, 0);
439 tsc = kvm_read_l1_tsc(vcpu, rdtsc());
440 return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64)
441 + hv->tsc_ref.tsc_offset;
444 static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
447 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
449 set_bit(stimer->index,
450 vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
451 kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
456 static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
458 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
460 trace_kvm_hv_stimer_cleanup(stimer_to_vcpu(stimer)->vcpu_id,
463 hrtimer_cancel(&stimer->timer);
464 clear_bit(stimer->index,
465 vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
466 stimer->msg_pending = false;
467 stimer->exp_time = 0;
470 static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
472 struct kvm_vcpu_hv_stimer *stimer;
474 stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
475 trace_kvm_hv_stimer_callback(stimer_to_vcpu(stimer)->vcpu_id,
477 stimer_mark_pending(stimer, true);
479 return HRTIMER_NORESTART;
483 * stimer_start() assumptions:
484 * a) stimer->count is not equal to 0
485 * b) stimer->config has HV_STIMER_ENABLE flag
487 static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
492 time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm);
493 ktime_now = ktime_get();
495 if (stimer->config & HV_STIMER_PERIODIC) {
496 if (stimer->exp_time) {
497 if (time_now >= stimer->exp_time) {
500 div64_u64_rem(time_now - stimer->exp_time,
501 stimer->count, &remainder);
503 time_now + (stimer->count - remainder);
506 stimer->exp_time = time_now + stimer->count;
508 trace_kvm_hv_stimer_start_periodic(
509 stimer_to_vcpu(stimer)->vcpu_id,
511 time_now, stimer->exp_time);
513 hrtimer_start(&stimer->timer,
514 ktime_add_ns(ktime_now,
515 100 * (stimer->exp_time - time_now)),
519 stimer->exp_time = stimer->count;
520 if (time_now >= stimer->count) {
522 * Expire timer according to Hypervisor Top-Level Functional
523 * specification v4(15.3.1):
524 * "If a one shot is enabled and the specified count is in
525 * the past, it will expire immediately."
527 stimer_mark_pending(stimer, false);
531 trace_kvm_hv_stimer_start_one_shot(stimer_to_vcpu(stimer)->vcpu_id,
533 time_now, stimer->count);
535 hrtimer_start(&stimer->timer,
536 ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
541 static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
544 trace_kvm_hv_stimer_set_config(stimer_to_vcpu(stimer)->vcpu_id,
545 stimer->index, config, host);
547 stimer_cleanup(stimer);
548 if ((stimer->config & HV_STIMER_ENABLE) && HV_STIMER_SINT(config) == 0)
549 config &= ~HV_STIMER_ENABLE;
550 stimer->config = config;
551 stimer_mark_pending(stimer, false);
555 static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
558 trace_kvm_hv_stimer_set_count(stimer_to_vcpu(stimer)->vcpu_id,
559 stimer->index, count, host);
561 stimer_cleanup(stimer);
562 stimer->count = count;
563 if (stimer->count == 0)
564 stimer->config &= ~HV_STIMER_ENABLE;
565 else if (stimer->config & HV_STIMER_AUTOENABLE)
566 stimer->config |= HV_STIMER_ENABLE;
567 stimer_mark_pending(stimer, false);
571 static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
573 *pconfig = stimer->config;
577 static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
579 *pcount = stimer->count;
583 static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
584 struct hv_message *src_msg)
586 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
589 struct hv_message *dst_msg;
591 struct hv_message_page *msg_page;
593 if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
596 gpa = synic->msg_page & PAGE_MASK;
597 page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
598 if (is_error_page(page))
601 msg_page = kmap_atomic(page);
602 dst_msg = &msg_page->sint_message[sint];
603 if (sync_cmpxchg(&dst_msg->header.message_type, HVMSG_NONE,
604 src_msg->header.message_type) != HVMSG_NONE) {
605 dst_msg->header.message_flags.msg_pending = 1;
608 memcpy(&dst_msg->u.payload, &src_msg->u.payload,
609 src_msg->header.payload_size);
610 dst_msg->header.message_type = src_msg->header.message_type;
611 dst_msg->header.payload_size = src_msg->header.payload_size;
612 r = synic_set_irq(synic, sint);
618 kunmap_atomic(msg_page);
619 kvm_release_page_dirty(page);
620 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
624 static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
626 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
627 struct hv_message *msg = &stimer->msg;
628 struct hv_timer_message_payload *payload =
629 (struct hv_timer_message_payload *)&msg->u.payload;
631 payload->expiration_time = stimer->exp_time;
632 payload->delivery_time = get_time_ref_counter(vcpu->kvm);
633 return synic_deliver_msg(vcpu_to_synic(vcpu),
634 HV_STIMER_SINT(stimer->config), msg);
637 static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
641 stimer->msg_pending = true;
642 r = stimer_send_msg(stimer);
643 trace_kvm_hv_stimer_expiration(stimer_to_vcpu(stimer)->vcpu_id,
646 stimer->msg_pending = false;
647 if (!(stimer->config & HV_STIMER_PERIODIC))
648 stimer->config &= ~HV_STIMER_ENABLE;
652 void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
654 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
655 struct kvm_vcpu_hv_stimer *stimer;
656 u64 time_now, exp_time;
659 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
660 if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
661 stimer = &hv_vcpu->stimer[i];
662 if (stimer->config & HV_STIMER_ENABLE) {
663 exp_time = stimer->exp_time;
667 get_time_ref_counter(vcpu->kvm);
668 if (time_now >= exp_time)
669 stimer_expiration(stimer);
672 if ((stimer->config & HV_STIMER_ENABLE) &&
674 if (!stimer->msg_pending)
675 stimer_start(stimer);
677 stimer_cleanup(stimer);
682 void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)
684 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
687 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
688 stimer_cleanup(&hv_vcpu->stimer[i]);
691 static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer)
693 struct hv_message *msg = &stimer->msg;
694 struct hv_timer_message_payload *payload =
695 (struct hv_timer_message_payload *)&msg->u.payload;
697 memset(&msg->header, 0, sizeof(msg->header));
698 msg->header.message_type = HVMSG_TIMER_EXPIRED;
699 msg->header.payload_size = sizeof(*payload);
701 payload->timer_index = stimer->index;
702 payload->expiration_time = 0;
703 payload->delivery_time = 0;
706 static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index)
708 memset(stimer, 0, sizeof(*stimer));
709 stimer->index = timer_index;
710 hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
711 stimer->timer.function = stimer_timer_callback;
712 stimer_prepare_msg(stimer);
715 void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
717 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
720 synic_init(&hv_vcpu->synic);
722 bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
723 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
724 stimer_init(&hv_vcpu->stimer[i], i);
727 void kvm_hv_vcpu_postcreate(struct kvm_vcpu *vcpu)
729 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
731 hv_vcpu->vp_index = kvm_vcpu_get_idx(vcpu);
734 int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages)
736 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
739 * Hyper-V SynIC auto EOI SINT's are
740 * not compatible with APICV, so deactivate APICV
742 kvm_vcpu_deactivate_apicv(vcpu);
743 synic->active = true;
744 synic->dont_zero_synic_pages = dont_zero_synic_pages;
748 static bool kvm_hv_msr_partition_wide(u32 msr)
753 case HV_X64_MSR_GUEST_OS_ID:
754 case HV_X64_MSR_HYPERCALL:
755 case HV_X64_MSR_REFERENCE_TSC:
756 case HV_X64_MSR_TIME_REF_COUNT:
757 case HV_X64_MSR_CRASH_CTL:
758 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
759 case HV_X64_MSR_RESET:
760 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
761 case HV_X64_MSR_TSC_EMULATION_CONTROL:
762 case HV_X64_MSR_TSC_EMULATION_STATUS:
770 static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
771 u32 index, u64 *pdata)
773 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
775 if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
778 *pdata = hv->hv_crash_param[index];
782 static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata)
784 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
786 *pdata = hv->hv_crash_ctl;
790 static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host)
792 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
795 hv->hv_crash_ctl = data & HV_X64_MSR_CRASH_CTL_NOTIFY;
797 if (!host && (data & HV_X64_MSR_CRASH_CTL_NOTIFY)) {
799 vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
800 hv->hv_crash_param[0],
801 hv->hv_crash_param[1],
802 hv->hv_crash_param[2],
803 hv->hv_crash_param[3],
804 hv->hv_crash_param[4]);
806 /* Send notification about crash to user space */
807 kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
813 static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
816 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
818 if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
821 hv->hv_crash_param[index] = data;
826 * The kvmclock and Hyper-V TSC page use similar formulas, and converting
827 * between them is possible:
830 * nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32)
834 * nsec/100 = ticks * scale / 2^64 + offset
836 * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula.
837 * By dividing the kvmclock formula by 100 and equating what's left we get:
838 * ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
839 * scale / 2^64 = tsc_to_system_mul * 2^(tsc_shift-32) / 100
840 * scale = tsc_to_system_mul * 2^(32+tsc_shift) / 100
842 * Now expand the kvmclock formula and divide by 100:
843 * nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32)
844 * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32)
846 * nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
847 * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100
848 * + system_time / 100
850 * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64:
851 * nsec/100 = ticks * scale / 2^64
852 * - tsc_timestamp * scale / 2^64
853 * + system_time / 100
855 * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out:
856 * offset = system_time / 100 - tsc_timestamp * scale / 2^64
858 * These two equivalencies are implemented in this function.
860 static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock,
861 HV_REFERENCE_TSC_PAGE *tsc_ref)
865 if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT))
869 * check if scale would overflow, if so we use the time ref counter
870 * tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64
871 * tsc_to_system_mul / 100 >= 2^(32-tsc_shift)
872 * tsc_to_system_mul >= 100 * 2^(32-tsc_shift)
874 max_mul = 100ull << (32 - hv_clock->tsc_shift);
875 if (hv_clock->tsc_to_system_mul >= max_mul)
879 * Otherwise compute the scale and offset according to the formulas
883 mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift),
884 hv_clock->tsc_to_system_mul,
887 tsc_ref->tsc_offset = hv_clock->system_time;
888 do_div(tsc_ref->tsc_offset, 100);
889 tsc_ref->tsc_offset -=
890 mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64);
894 void kvm_hv_setup_tsc_page(struct kvm *kvm,
895 struct pvclock_vcpu_time_info *hv_clock)
897 struct kvm_hv *hv = &kvm->arch.hyperv;
901 BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence));
902 BUILD_BUG_ON(offsetof(HV_REFERENCE_TSC_PAGE, tsc_sequence) != 0);
904 if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
907 mutex_lock(&kvm->arch.hyperv.hv_lock);
908 if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
911 gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
913 * Because the TSC parameters only vary when there is a
914 * change in the master clock, do not bother with caching.
916 if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn),
917 &tsc_seq, sizeof(tsc_seq))))
921 * While we're computing and writing the parameters, force the
922 * guest to use the time reference count MSR.
924 hv->tsc_ref.tsc_sequence = 0;
925 if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
926 &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
929 if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref))
932 /* Ensure sequence is zero before writing the rest of the struct. */
934 if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
938 * Now switch to the TSC page mechanism by writing the sequence.
941 if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0)
944 /* Write the struct entirely before the non-zero sequence. */
947 hv->tsc_ref.tsc_sequence = tsc_seq;
948 kvm_write_guest(kvm, gfn_to_gpa(gfn),
949 &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence));
951 mutex_unlock(&kvm->arch.hyperv.hv_lock);
954 static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
957 struct kvm *kvm = vcpu->kvm;
958 struct kvm_hv *hv = &kvm->arch.hyperv;
961 case HV_X64_MSR_GUEST_OS_ID:
962 hv->hv_guest_os_id = data;
963 /* setting guest os id to zero disables hypercall page */
964 if (!hv->hv_guest_os_id)
965 hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
967 case HV_X64_MSR_HYPERCALL: {
972 /* if guest os id is not set hypercall should remain disabled */
973 if (!hv->hv_guest_os_id)
975 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
976 hv->hv_hypercall = data;
979 gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
980 addr = gfn_to_hva(kvm, gfn);
981 if (kvm_is_error_hva(addr))
983 kvm_x86_ops->patch_hypercall(vcpu, instructions);
984 ((unsigned char *)instructions)[3] = 0xc3; /* ret */
985 if (__copy_to_user((void __user *)addr, instructions, 4))
987 hv->hv_hypercall = data;
988 mark_page_dirty(kvm, gfn);
991 case HV_X64_MSR_REFERENCE_TSC:
992 hv->hv_tsc_page = data;
993 if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)
994 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
996 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
997 return kvm_hv_msr_set_crash_data(vcpu,
998 msr - HV_X64_MSR_CRASH_P0,
1000 case HV_X64_MSR_CRASH_CTL:
1001 return kvm_hv_msr_set_crash_ctl(vcpu, data, host);
1002 case HV_X64_MSR_RESET:
1004 vcpu_debug(vcpu, "hyper-v reset requested\n");
1005 kvm_make_request(KVM_REQ_HV_RESET, vcpu);
1008 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1009 hv->hv_reenlightenment_control = data;
1011 case HV_X64_MSR_TSC_EMULATION_CONTROL:
1012 hv->hv_tsc_emulation_control = data;
1014 case HV_X64_MSR_TSC_EMULATION_STATUS:
1015 hv->hv_tsc_emulation_status = data;
1018 vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
1025 /* Calculate cpu time spent by current task in 100ns units */
1026 static u64 current_task_runtime_100ns(void)
1030 task_cputime_adjusted(current, &utime, &stime);
1032 return div_u64(utime + stime, 100);
1035 static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
1037 struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
1040 case HV_X64_MSR_VP_INDEX:
1043 hv->vp_index = (u32)data;
1045 case HV_X64_MSR_VP_ASSIST_PAGE: {
1049 if (!(data & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE)) {
1050 hv->hv_vapic = data;
1051 if (kvm_lapic_enable_pv_eoi(vcpu, 0))
1055 gfn = data >> HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT;
1056 addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
1057 if (kvm_is_error_hva(addr))
1059 if (__clear_user((void __user *)addr, PAGE_SIZE))
1061 hv->hv_vapic = data;
1062 kvm_vcpu_mark_page_dirty(vcpu, gfn);
1063 if (kvm_lapic_enable_pv_eoi(vcpu,
1064 gfn_to_gpa(gfn) | KVM_MSR_ENABLED))
1068 case HV_X64_MSR_EOI:
1069 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1070 case HV_X64_MSR_ICR:
1071 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1072 case HV_X64_MSR_TPR:
1073 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
1074 case HV_X64_MSR_VP_RUNTIME:
1077 hv->runtime_offset = data - current_task_runtime_100ns();
1079 case HV_X64_MSR_SCONTROL:
1080 case HV_X64_MSR_SVERSION:
1081 case HV_X64_MSR_SIEFP:
1082 case HV_X64_MSR_SIMP:
1083 case HV_X64_MSR_EOM:
1084 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
1085 return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host);
1086 case HV_X64_MSR_STIMER0_CONFIG:
1087 case HV_X64_MSR_STIMER1_CONFIG:
1088 case HV_X64_MSR_STIMER2_CONFIG:
1089 case HV_X64_MSR_STIMER3_CONFIG: {
1090 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
1092 return stimer_set_config(vcpu_to_stimer(vcpu, timer_index),
1095 case HV_X64_MSR_STIMER0_COUNT:
1096 case HV_X64_MSR_STIMER1_COUNT:
1097 case HV_X64_MSR_STIMER2_COUNT:
1098 case HV_X64_MSR_STIMER3_COUNT: {
1099 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
1101 return stimer_set_count(vcpu_to_stimer(vcpu, timer_index),
1105 vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
1113 static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1116 struct kvm *kvm = vcpu->kvm;
1117 struct kvm_hv *hv = &kvm->arch.hyperv;
1120 case HV_X64_MSR_GUEST_OS_ID:
1121 data = hv->hv_guest_os_id;
1123 case HV_X64_MSR_HYPERCALL:
1124 data = hv->hv_hypercall;
1126 case HV_X64_MSR_TIME_REF_COUNT:
1127 data = get_time_ref_counter(kvm);
1129 case HV_X64_MSR_REFERENCE_TSC:
1130 data = hv->hv_tsc_page;
1132 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1133 return kvm_hv_msr_get_crash_data(vcpu,
1134 msr - HV_X64_MSR_CRASH_P0,
1136 case HV_X64_MSR_CRASH_CTL:
1137 return kvm_hv_msr_get_crash_ctl(vcpu, pdata);
1138 case HV_X64_MSR_RESET:
1141 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1142 data = hv->hv_reenlightenment_control;
1144 case HV_X64_MSR_TSC_EMULATION_CONTROL:
1145 data = hv->hv_tsc_emulation_control;
1147 case HV_X64_MSR_TSC_EMULATION_STATUS:
1148 data = hv->hv_tsc_emulation_status;
1151 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1159 static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1162 struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
1165 case HV_X64_MSR_VP_INDEX:
1166 data = hv->vp_index;
1168 case HV_X64_MSR_EOI:
1169 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1170 case HV_X64_MSR_ICR:
1171 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1172 case HV_X64_MSR_TPR:
1173 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
1174 case HV_X64_MSR_VP_ASSIST_PAGE:
1175 data = hv->hv_vapic;
1177 case HV_X64_MSR_VP_RUNTIME:
1178 data = current_task_runtime_100ns() + hv->runtime_offset;
1180 case HV_X64_MSR_SCONTROL:
1181 case HV_X64_MSR_SVERSION:
1182 case HV_X64_MSR_SIEFP:
1183 case HV_X64_MSR_SIMP:
1184 case HV_X64_MSR_EOM:
1185 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
1186 return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata);
1187 case HV_X64_MSR_STIMER0_CONFIG:
1188 case HV_X64_MSR_STIMER1_CONFIG:
1189 case HV_X64_MSR_STIMER2_CONFIG:
1190 case HV_X64_MSR_STIMER3_CONFIG: {
1191 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
1193 return stimer_get_config(vcpu_to_stimer(vcpu, timer_index),
1196 case HV_X64_MSR_STIMER0_COUNT:
1197 case HV_X64_MSR_STIMER1_COUNT:
1198 case HV_X64_MSR_STIMER2_COUNT:
1199 case HV_X64_MSR_STIMER3_COUNT: {
1200 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
1202 return stimer_get_count(vcpu_to_stimer(vcpu, timer_index),
1205 case HV_X64_MSR_TSC_FREQUENCY:
1206 data = (u64)vcpu->arch.virtual_tsc_khz * 1000;
1208 case HV_X64_MSR_APIC_FREQUENCY:
1209 data = APIC_BUS_FREQUENCY;
1212 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1219 int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
1221 if (kvm_hv_msr_partition_wide(msr)) {
1224 mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
1225 r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
1226 mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
1229 return kvm_hv_set_msr(vcpu, msr, data, host);
1232 int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1234 if (kvm_hv_msr_partition_wide(msr)) {
1237 mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
1238 r = kvm_hv_get_msr_pw(vcpu, msr, pdata);
1239 mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
1242 return kvm_hv_get_msr(vcpu, msr, pdata);
1245 bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1247 return READ_ONCE(kvm->arch.hyperv.hv_hypercall) & HV_X64_MSR_HYPERCALL_ENABLE;
1250 static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
1254 longmode = is_64_bit_mode(vcpu);
1256 kvm_register_write(vcpu, VCPU_REGS_RAX, result);
1258 kvm_register_write(vcpu, VCPU_REGS_RDX, result >> 32);
1259 kvm_register_write(vcpu, VCPU_REGS_RAX, result & 0xffffffff);
1263 static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
1265 struct kvm_run *run = vcpu->run;
1267 kvm_hv_hypercall_set_result(vcpu, run->hyperv.u.hcall.result);
1268 return kvm_skip_emulated_instruction(vcpu);
1271 static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, bool fast, u64 param)
1273 struct eventfd_ctx *eventfd;
1275 if (unlikely(!fast)) {
1279 if ((gpa & (__alignof__(param) - 1)) ||
1280 offset_in_page(gpa) + sizeof(param) > PAGE_SIZE)
1281 return HV_STATUS_INVALID_ALIGNMENT;
1283 ret = kvm_vcpu_read_guest(vcpu, gpa, ¶m, sizeof(param));
1285 return HV_STATUS_INVALID_ALIGNMENT;
1289 * Per spec, bits 32-47 contain the extra "flag number". However, we
1290 * have no use for it, and in all known usecases it is zero, so just
1291 * report lookup failure if it isn't.
1293 if (param & 0xffff00000000ULL)
1294 return HV_STATUS_INVALID_PORT_ID;
1295 /* remaining bits are reserved-zero */
1296 if (param & ~KVM_HYPERV_CONN_ID_MASK)
1297 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1299 /* the eventfd is protected by vcpu->kvm->srcu, but conn_to_evt isn't */
1301 eventfd = idr_find(&vcpu->kvm->arch.hyperv.conn_to_evt, param);
1304 return HV_STATUS_INVALID_PORT_ID;
1306 eventfd_signal(eventfd, 1);
1307 return HV_STATUS_SUCCESS;
1310 int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
1312 u64 param, ingpa, outgpa, ret = HV_STATUS_SUCCESS;
1313 uint16_t code, rep_idx, rep_cnt;
1314 bool fast, longmode;
1317 * hypercall generates UD from non zero cpl and real mode
1320 if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
1321 kvm_queue_exception(vcpu, UD_VECTOR);
1325 longmode = is_64_bit_mode(vcpu);
1328 param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
1329 (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
1330 ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
1331 (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
1332 outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
1333 (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
1335 #ifdef CONFIG_X86_64
1337 param = kvm_register_read(vcpu, VCPU_REGS_RCX);
1338 ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
1339 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
1343 code = param & 0xffff;
1344 fast = (param >> 16) & 0x1;
1345 rep_cnt = (param >> 32) & 0xfff;
1346 rep_idx = (param >> 48) & 0xfff;
1348 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
1350 /* Hypercall continuation is not supported yet */
1351 if (rep_cnt || rep_idx) {
1352 ret = HV_STATUS_INVALID_HYPERCALL_CODE;
1357 case HVCALL_NOTIFY_LONG_SPIN_WAIT:
1358 kvm_vcpu_on_spin(vcpu, true);
1360 case HVCALL_SIGNAL_EVENT:
1361 ret = kvm_hvcall_signal_event(vcpu, fast, ingpa);
1362 if (ret != HV_STATUS_INVALID_PORT_ID)
1364 /* maybe userspace knows this conn_id: fall through */
1365 case HVCALL_POST_MESSAGE:
1366 /* don't bother userspace if it has no way to handle it */
1367 if (!vcpu_to_synic(vcpu)->active) {
1368 ret = HV_STATUS_INVALID_HYPERCALL_CODE;
1371 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
1372 vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
1373 vcpu->run->hyperv.u.hcall.input = param;
1374 vcpu->run->hyperv.u.hcall.params[0] = ingpa;
1375 vcpu->run->hyperv.u.hcall.params[1] = outgpa;
1376 vcpu->arch.complete_userspace_io =
1377 kvm_hv_hypercall_complete_userspace;
1380 ret = HV_STATUS_INVALID_HYPERCALL_CODE;
1385 kvm_hv_hypercall_set_result(vcpu, ret);
1389 void kvm_hv_init_vm(struct kvm *kvm)
1391 mutex_init(&kvm->arch.hyperv.hv_lock);
1392 idr_init(&kvm->arch.hyperv.conn_to_evt);
1395 void kvm_hv_destroy_vm(struct kvm *kvm)
1397 struct eventfd_ctx *eventfd;
1400 idr_for_each_entry(&kvm->arch.hyperv.conn_to_evt, eventfd, i)
1401 eventfd_ctx_put(eventfd);
1402 idr_destroy(&kvm->arch.hyperv.conn_to_evt);
1405 static int kvm_hv_eventfd_assign(struct kvm *kvm, u32 conn_id, int fd)
1407 struct kvm_hv *hv = &kvm->arch.hyperv;
1408 struct eventfd_ctx *eventfd;
1411 eventfd = eventfd_ctx_fdget(fd);
1412 if (IS_ERR(eventfd))
1413 return PTR_ERR(eventfd);
1415 mutex_lock(&hv->hv_lock);
1416 ret = idr_alloc(&hv->conn_to_evt, eventfd, conn_id, conn_id + 1,
1418 mutex_unlock(&hv->hv_lock);
1425 eventfd_ctx_put(eventfd);
1429 static int kvm_hv_eventfd_deassign(struct kvm *kvm, u32 conn_id)
1431 struct kvm_hv *hv = &kvm->arch.hyperv;
1432 struct eventfd_ctx *eventfd;
1434 mutex_lock(&hv->hv_lock);
1435 eventfd = idr_remove(&hv->conn_to_evt, conn_id);
1436 mutex_unlock(&hv->hv_lock);
1441 synchronize_srcu(&kvm->srcu);
1442 eventfd_ctx_put(eventfd);
1446 int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args)
1448 if ((args->flags & ~KVM_HYPERV_EVENTFD_DEASSIGN) ||
1449 (args->conn_id & ~KVM_HYPERV_CONN_ID_MASK))
1452 if (args->flags == KVM_HYPERV_EVENTFD_DEASSIGN)
1453 return kvm_hv_eventfd_deassign(kvm, args->conn_id);
1454 return kvm_hv_eventfd_assign(kvm, args->conn_id, args->fd);