1 // SPDX-License-Identifier: GPL-2.0-only
3 * KVM Microsoft Hyper-V emulation
5 * derived from arch/x86/kvm/x86.c
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright (C) 2008 Qumranet, Inc.
9 * Copyright IBM Corporation, 2008
10 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
11 * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com>
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 * Amit Shah <amit.shah@qumranet.com>
17 * Ben-Ami Yassour <benami@il.ibm.com>
18 * Andrey Smetanin <asmetanin@virtuozzo.com>
28 #include <linux/cpu.h>
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>
41 #define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, 64)
43 static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
46 static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
48 return atomic64_read(&synic->sint[sint]);
51 static inline int synic_get_sint_vector(u64 sint_value)
53 if (sint_value & HV_SYNIC_SINT_MASKED)
55 return sint_value & HV_SYNIC_SINT_VECTOR_MASK;
58 static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic,
63 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
64 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
70 static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic,
76 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
77 sint_value = synic_read_sint(synic, i);
78 if (synic_get_sint_vector(sint_value) == vector &&
79 sint_value & HV_SYNIC_SINT_AUTO_EOI)
85 static void synic_update_vector(struct kvm_vcpu_hv_synic *synic,
88 struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
89 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
90 bool auto_eoi_old, auto_eoi_new;
92 if (vector < HV_SYNIC_FIRST_VALID_VECTOR)
95 if (synic_has_vector_connected(synic, vector))
96 __set_bit(vector, synic->vec_bitmap);
98 __clear_bit(vector, synic->vec_bitmap);
100 auto_eoi_old = !bitmap_empty(synic->auto_eoi_bitmap, 256);
102 if (synic_has_vector_auto_eoi(synic, vector))
103 __set_bit(vector, synic->auto_eoi_bitmap);
105 __clear_bit(vector, synic->auto_eoi_bitmap);
107 auto_eoi_new = !bitmap_empty(synic->auto_eoi_bitmap, 256);
109 if (auto_eoi_old == auto_eoi_new)
115 down_write(&vcpu->kvm->arch.apicv_update_lock);
118 hv->synic_auto_eoi_used++;
120 hv->synic_auto_eoi_used--;
123 * Inhibit APICv if any vCPU is using SynIC's AutoEOI, which relies on
124 * the hypervisor to manually inject IRQs.
126 __kvm_set_or_clear_apicv_inhibit(vcpu->kvm,
127 APICV_INHIBIT_REASON_HYPERV,
128 !!hv->synic_auto_eoi_used);
130 up_write(&vcpu->kvm->arch.apicv_update_lock);
133 static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint,
136 int vector, old_vector;
139 vector = data & HV_SYNIC_SINT_VECTOR_MASK;
140 masked = data & HV_SYNIC_SINT_MASKED;
143 * Valid vectors are 16-255, however, nested Hyper-V attempts to write
144 * default '0x10000' value on boot and this should not #GP. We need to
145 * allow zero-initing the register from host as well.
147 if (vector < HV_SYNIC_FIRST_VALID_VECTOR && !host && !masked)
150 * Guest may configure multiple SINTs to use the same vector, so
151 * we maintain a bitmap of vectors handled by synic, and a
152 * bitmap of vectors with auto-eoi behavior. The bitmaps are
153 * updated here, and atomically queried on fast paths.
155 old_vector = synic_read_sint(synic, sint) & HV_SYNIC_SINT_VECTOR_MASK;
157 atomic64_set(&synic->sint[sint], data);
159 synic_update_vector(synic, old_vector);
161 synic_update_vector(synic, vector);
163 /* Load SynIC vectors into EOI exit bitmap */
164 kvm_make_request(KVM_REQ_SCAN_IOAPIC, hv_synic_to_vcpu(synic));
168 static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx)
170 struct kvm_vcpu *vcpu = NULL;
173 if (vpidx >= KVM_MAX_VCPUS)
176 vcpu = kvm_get_vcpu(kvm, vpidx);
177 if (vcpu && kvm_hv_get_vpindex(vcpu) == vpidx)
179 kvm_for_each_vcpu(i, vcpu, kvm)
180 if (kvm_hv_get_vpindex(vcpu) == vpidx)
185 static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vpidx)
187 struct kvm_vcpu *vcpu;
188 struct kvm_vcpu_hv_synic *synic;
190 vcpu = get_vcpu_by_vpidx(kvm, vpidx);
191 if (!vcpu || !to_hv_vcpu(vcpu))
193 synic = to_hv_synic(vcpu);
194 return (synic->active) ? synic : NULL;
197 static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
199 struct kvm *kvm = vcpu->kvm;
200 struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu);
201 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
202 struct kvm_vcpu_hv_stimer *stimer;
205 trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint);
207 /* Try to deliver pending Hyper-V SynIC timers messages */
208 for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) {
209 stimer = &hv_vcpu->stimer[idx];
210 if (stimer->msg_pending && stimer->config.enable &&
211 !stimer->config.direct_mode &&
212 stimer->config.sintx == sint)
213 stimer_mark_pending(stimer, false);
216 idx = srcu_read_lock(&kvm->irq_srcu);
217 gsi = atomic_read(&synic->sint_to_gsi[sint]);
219 kvm_notify_acked_gsi(kvm, gsi);
220 srcu_read_unlock(&kvm->irq_srcu, idx);
223 static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
225 struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
226 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
228 hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
229 hv_vcpu->exit.u.synic.msr = msr;
230 hv_vcpu->exit.u.synic.control = synic->control;
231 hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
232 hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
234 kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
237 static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
238 u32 msr, u64 data, bool host)
240 struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
243 if (!synic->active && (!host || data))
246 trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host);
250 case HV_X64_MSR_SCONTROL:
251 synic->control = data;
253 synic_exit(synic, msr);
255 case HV_X64_MSR_SVERSION:
260 synic->version = data;
262 case HV_X64_MSR_SIEFP:
263 if ((data & HV_SYNIC_SIEFP_ENABLE) && !host &&
264 !synic->dont_zero_synic_pages)
265 if (kvm_clear_guest(vcpu->kvm,
266 data & PAGE_MASK, PAGE_SIZE)) {
270 synic->evt_page = data;
272 synic_exit(synic, msr);
274 case HV_X64_MSR_SIMP:
275 if ((data & HV_SYNIC_SIMP_ENABLE) && !host &&
276 !synic->dont_zero_synic_pages)
277 if (kvm_clear_guest(vcpu->kvm,
278 data & PAGE_MASK, PAGE_SIZE)) {
282 synic->msg_page = data;
284 synic_exit(synic, msr);
286 case HV_X64_MSR_EOM: {
292 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
293 kvm_hv_notify_acked_sint(vcpu, i);
296 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
297 ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host);
306 static bool kvm_hv_is_syndbg_enabled(struct kvm_vcpu *vcpu)
308 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
310 return hv_vcpu->cpuid_cache.syndbg_cap_eax &
311 HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING;
314 static int kvm_hv_syndbg_complete_userspace(struct kvm_vcpu *vcpu)
316 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
318 if (vcpu->run->hyperv.u.syndbg.msr == HV_X64_MSR_SYNDBG_CONTROL)
319 hv->hv_syndbg.control.status =
320 vcpu->run->hyperv.u.syndbg.status;
324 static void syndbg_exit(struct kvm_vcpu *vcpu, u32 msr)
326 struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu);
327 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
329 hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNDBG;
330 hv_vcpu->exit.u.syndbg.msr = msr;
331 hv_vcpu->exit.u.syndbg.control = syndbg->control.control;
332 hv_vcpu->exit.u.syndbg.send_page = syndbg->control.send_page;
333 hv_vcpu->exit.u.syndbg.recv_page = syndbg->control.recv_page;
334 hv_vcpu->exit.u.syndbg.pending_page = syndbg->control.pending_page;
335 vcpu->arch.complete_userspace_io =
336 kvm_hv_syndbg_complete_userspace;
338 kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
341 static int syndbg_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
343 struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu);
345 if (!kvm_hv_is_syndbg_enabled(vcpu) && !host)
348 trace_kvm_hv_syndbg_set_msr(vcpu->vcpu_id,
349 to_hv_vcpu(vcpu)->vp_index, msr, data);
351 case HV_X64_MSR_SYNDBG_CONTROL:
352 syndbg->control.control = data;
354 syndbg_exit(vcpu, msr);
356 case HV_X64_MSR_SYNDBG_STATUS:
357 syndbg->control.status = data;
359 case HV_X64_MSR_SYNDBG_SEND_BUFFER:
360 syndbg->control.send_page = data;
362 case HV_X64_MSR_SYNDBG_RECV_BUFFER:
363 syndbg->control.recv_page = data;
365 case HV_X64_MSR_SYNDBG_PENDING_BUFFER:
366 syndbg->control.pending_page = data;
368 syndbg_exit(vcpu, msr);
370 case HV_X64_MSR_SYNDBG_OPTIONS:
371 syndbg->options = data;
380 static int syndbg_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
382 struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu);
384 if (!kvm_hv_is_syndbg_enabled(vcpu) && !host)
388 case HV_X64_MSR_SYNDBG_CONTROL:
389 *pdata = syndbg->control.control;
391 case HV_X64_MSR_SYNDBG_STATUS:
392 *pdata = syndbg->control.status;
394 case HV_X64_MSR_SYNDBG_SEND_BUFFER:
395 *pdata = syndbg->control.send_page;
397 case HV_X64_MSR_SYNDBG_RECV_BUFFER:
398 *pdata = syndbg->control.recv_page;
400 case HV_X64_MSR_SYNDBG_PENDING_BUFFER:
401 *pdata = syndbg->control.pending_page;
403 case HV_X64_MSR_SYNDBG_OPTIONS:
404 *pdata = syndbg->options;
410 trace_kvm_hv_syndbg_get_msr(vcpu->vcpu_id, kvm_hv_get_vpindex(vcpu), msr, *pdata);
415 static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata,
420 if (!synic->active && !host)
425 case HV_X64_MSR_SCONTROL:
426 *pdata = synic->control;
428 case HV_X64_MSR_SVERSION:
429 *pdata = synic->version;
431 case HV_X64_MSR_SIEFP:
432 *pdata = synic->evt_page;
434 case HV_X64_MSR_SIMP:
435 *pdata = synic->msg_page;
440 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
441 *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
450 static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
452 struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
453 struct kvm_lapic_irq irq;
456 if (KVM_BUG_ON(!lapic_in_kernel(vcpu), vcpu->kvm))
459 if (sint >= ARRAY_SIZE(synic->sint))
462 vector = synic_get_sint_vector(synic_read_sint(synic, sint));
466 memset(&irq, 0, sizeof(irq));
467 irq.shorthand = APIC_DEST_SELF;
468 irq.dest_mode = APIC_DEST_PHYSICAL;
469 irq.delivery_mode = APIC_DM_FIXED;
473 ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq, NULL);
474 trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret);
478 int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vpidx, u32 sint)
480 struct kvm_vcpu_hv_synic *synic;
482 synic = synic_get(kvm, vpidx);
486 return synic_set_irq(synic, sint);
489 void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
491 struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu);
494 trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector);
496 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
497 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
498 kvm_hv_notify_acked_sint(vcpu, i);
501 static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vpidx, u32 sint, int gsi)
503 struct kvm_vcpu_hv_synic *synic;
505 synic = synic_get(kvm, vpidx);
509 if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
512 atomic_set(&synic->sint_to_gsi[sint], gsi);
516 void kvm_hv_irq_routing_update(struct kvm *kvm)
518 struct kvm_irq_routing_table *irq_rt;
519 struct kvm_kernel_irq_routing_entry *e;
522 irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
523 lockdep_is_held(&kvm->irq_lock));
525 for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
526 hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
527 if (e->type == KVM_IRQ_ROUTING_HV_SINT)
528 kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
529 e->hv_sint.sint, gsi);
534 static void synic_init(struct kvm_vcpu_hv_synic *synic)
538 memset(synic, 0, sizeof(*synic));
539 synic->version = HV_SYNIC_VERSION_1;
540 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
541 atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
542 atomic_set(&synic->sint_to_gsi[i], -1);
546 static u64 get_time_ref_counter(struct kvm *kvm)
548 struct kvm_hv *hv = to_kvm_hv(kvm);
549 struct kvm_vcpu *vcpu;
553 * Fall back to get_kvmclock_ns() when TSC page hasn't been set up,
554 * is broken, disabled or being updated.
556 if (hv->hv_tsc_page_status != HV_TSC_PAGE_SET)
557 return div_u64(get_kvmclock_ns(kvm), 100);
559 vcpu = kvm_get_vcpu(kvm, 0);
560 tsc = kvm_read_l1_tsc(vcpu, rdtsc());
561 return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64)
562 + hv->tsc_ref.tsc_offset;
565 static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
568 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
570 set_bit(stimer->index,
571 to_hv_vcpu(vcpu)->stimer_pending_bitmap);
572 kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
577 static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
579 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
581 trace_kvm_hv_stimer_cleanup(hv_stimer_to_vcpu(stimer)->vcpu_id,
584 hrtimer_cancel(&stimer->timer);
585 clear_bit(stimer->index,
586 to_hv_vcpu(vcpu)->stimer_pending_bitmap);
587 stimer->msg_pending = false;
588 stimer->exp_time = 0;
591 static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
593 struct kvm_vcpu_hv_stimer *stimer;
595 stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
596 trace_kvm_hv_stimer_callback(hv_stimer_to_vcpu(stimer)->vcpu_id,
598 stimer_mark_pending(stimer, true);
600 return HRTIMER_NORESTART;
604 * stimer_start() assumptions:
605 * a) stimer->count is not equal to 0
606 * b) stimer->config has HV_STIMER_ENABLE flag
608 static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
613 time_now = get_time_ref_counter(hv_stimer_to_vcpu(stimer)->kvm);
614 ktime_now = ktime_get();
616 if (stimer->config.periodic) {
617 if (stimer->exp_time) {
618 if (time_now >= stimer->exp_time) {
621 div64_u64_rem(time_now - stimer->exp_time,
622 stimer->count, &remainder);
624 time_now + (stimer->count - remainder);
627 stimer->exp_time = time_now + stimer->count;
629 trace_kvm_hv_stimer_start_periodic(
630 hv_stimer_to_vcpu(stimer)->vcpu_id,
632 time_now, stimer->exp_time);
634 hrtimer_start(&stimer->timer,
635 ktime_add_ns(ktime_now,
636 100 * (stimer->exp_time - time_now)),
640 stimer->exp_time = stimer->count;
641 if (time_now >= stimer->count) {
643 * Expire timer according to Hypervisor Top-Level Functional
644 * specification v4(15.3.1):
645 * "If a one shot is enabled and the specified count is in
646 * the past, it will expire immediately."
648 stimer_mark_pending(stimer, false);
652 trace_kvm_hv_stimer_start_one_shot(hv_stimer_to_vcpu(stimer)->vcpu_id,
654 time_now, stimer->count);
656 hrtimer_start(&stimer->timer,
657 ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
662 static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
665 union hv_stimer_config new_config = {.as_uint64 = config},
666 old_config = {.as_uint64 = stimer->config.as_uint64};
667 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
668 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
669 struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu);
671 if (!synic->active && (!host || config))
674 if (unlikely(!host && hv_vcpu->enforce_cpuid && new_config.direct_mode &&
675 !(hv_vcpu->cpuid_cache.features_edx &
676 HV_STIMER_DIRECT_MODE_AVAILABLE)))
679 trace_kvm_hv_stimer_set_config(hv_stimer_to_vcpu(stimer)->vcpu_id,
680 stimer->index, config, host);
682 stimer_cleanup(stimer);
683 if (old_config.enable &&
684 !new_config.direct_mode && new_config.sintx == 0)
685 new_config.enable = 0;
686 stimer->config.as_uint64 = new_config.as_uint64;
688 if (stimer->config.enable)
689 stimer_mark_pending(stimer, false);
694 static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
697 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
698 struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu);
700 if (!synic->active && (!host || count))
703 trace_kvm_hv_stimer_set_count(hv_stimer_to_vcpu(stimer)->vcpu_id,
704 stimer->index, count, host);
706 stimer_cleanup(stimer);
707 stimer->count = count;
708 if (stimer->count == 0)
709 stimer->config.enable = 0;
710 else if (stimer->config.auto_enable)
711 stimer->config.enable = 1;
713 if (stimer->config.enable)
714 stimer_mark_pending(stimer, false);
719 static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
721 *pconfig = stimer->config.as_uint64;
725 static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
727 *pcount = stimer->count;
731 static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
732 struct hv_message *src_msg, bool no_retry)
734 struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
735 int msg_off = offsetof(struct hv_message_page, sint_message[sint]);
737 struct hv_message_header hv_hdr;
740 if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
743 msg_page_gfn = synic->msg_page >> PAGE_SHIFT;
746 * Strictly following the spec-mandated ordering would assume setting
747 * .msg_pending before checking .message_type. However, this function
748 * is only called in vcpu context so the entire update is atomic from
749 * guest POV and thus the exact order here doesn't matter.
751 r = kvm_vcpu_read_guest_page(vcpu, msg_page_gfn, &hv_hdr.message_type,
752 msg_off + offsetof(struct hv_message,
753 header.message_type),
754 sizeof(hv_hdr.message_type));
758 if (hv_hdr.message_type != HVMSG_NONE) {
762 hv_hdr.message_flags.msg_pending = 1;
763 r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn,
764 &hv_hdr.message_flags,
766 offsetof(struct hv_message,
767 header.message_flags),
768 sizeof(hv_hdr.message_flags));
774 r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn, src_msg, msg_off,
775 sizeof(src_msg->header) +
776 src_msg->header.payload_size);
780 r = synic_set_irq(synic, sint);
788 static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
790 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
791 struct hv_message *msg = &stimer->msg;
792 struct hv_timer_message_payload *payload =
793 (struct hv_timer_message_payload *)&msg->u.payload;
796 * To avoid piling up periodic ticks, don't retry message
797 * delivery for them (within "lazy" lost ticks policy).
799 bool no_retry = stimer->config.periodic;
801 payload->expiration_time = stimer->exp_time;
802 payload->delivery_time = get_time_ref_counter(vcpu->kvm);
803 return synic_deliver_msg(to_hv_synic(vcpu),
804 stimer->config.sintx, msg,
808 static int stimer_notify_direct(struct kvm_vcpu_hv_stimer *stimer)
810 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
811 struct kvm_lapic_irq irq = {
812 .delivery_mode = APIC_DM_FIXED,
813 .vector = stimer->config.apic_vector
816 if (lapic_in_kernel(vcpu))
817 return !kvm_apic_set_irq(vcpu, &irq, NULL);
821 static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
823 int r, direct = stimer->config.direct_mode;
825 stimer->msg_pending = true;
827 r = stimer_send_msg(stimer);
829 r = stimer_notify_direct(stimer);
830 trace_kvm_hv_stimer_expiration(hv_stimer_to_vcpu(stimer)->vcpu_id,
831 stimer->index, direct, r);
833 stimer->msg_pending = false;
834 if (!(stimer->config.periodic))
835 stimer->config.enable = 0;
839 void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
841 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
842 struct kvm_vcpu_hv_stimer *stimer;
843 u64 time_now, exp_time;
849 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
850 if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
851 stimer = &hv_vcpu->stimer[i];
852 if (stimer->config.enable) {
853 exp_time = stimer->exp_time;
857 get_time_ref_counter(vcpu->kvm);
858 if (time_now >= exp_time)
859 stimer_expiration(stimer);
862 if ((stimer->config.enable) &&
864 if (!stimer->msg_pending)
865 stimer_start(stimer);
867 stimer_cleanup(stimer);
872 void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)
874 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
880 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
881 stimer_cleanup(&hv_vcpu->stimer[i]);
884 vcpu->arch.hyperv = NULL;
887 bool kvm_hv_assist_page_enabled(struct kvm_vcpu *vcpu)
889 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
894 if (!(hv_vcpu->hv_vapic & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE))
896 return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
898 EXPORT_SYMBOL_GPL(kvm_hv_assist_page_enabled);
900 bool kvm_hv_get_assist_page(struct kvm_vcpu *vcpu,
901 struct hv_vp_assist_page *assist_page)
903 if (!kvm_hv_assist_page_enabled(vcpu))
905 return !kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data,
906 assist_page, sizeof(*assist_page));
908 EXPORT_SYMBOL_GPL(kvm_hv_get_assist_page);
910 static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer)
912 struct hv_message *msg = &stimer->msg;
913 struct hv_timer_message_payload *payload =
914 (struct hv_timer_message_payload *)&msg->u.payload;
916 memset(&msg->header, 0, sizeof(msg->header));
917 msg->header.message_type = HVMSG_TIMER_EXPIRED;
918 msg->header.payload_size = sizeof(*payload);
920 payload->timer_index = stimer->index;
921 payload->expiration_time = 0;
922 payload->delivery_time = 0;
925 static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index)
927 memset(stimer, 0, sizeof(*stimer));
928 stimer->index = timer_index;
929 hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
930 stimer->timer.function = stimer_timer_callback;
931 stimer_prepare_msg(stimer);
934 int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
936 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
942 hv_vcpu = kzalloc(sizeof(struct kvm_vcpu_hv), GFP_KERNEL_ACCOUNT);
946 vcpu->arch.hyperv = hv_vcpu;
947 hv_vcpu->vcpu = vcpu;
949 synic_init(&hv_vcpu->synic);
951 bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
952 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
953 stimer_init(&hv_vcpu->stimer[i], i);
955 hv_vcpu->vp_index = vcpu->vcpu_idx;
960 int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages)
962 struct kvm_vcpu_hv_synic *synic;
965 r = kvm_hv_vcpu_init(vcpu);
969 synic = to_hv_synic(vcpu);
971 synic->active = true;
972 synic->dont_zero_synic_pages = dont_zero_synic_pages;
973 synic->control = HV_SYNIC_CONTROL_ENABLE;
977 static bool kvm_hv_msr_partition_wide(u32 msr)
982 case HV_X64_MSR_GUEST_OS_ID:
983 case HV_X64_MSR_HYPERCALL:
984 case HV_X64_MSR_REFERENCE_TSC:
985 case HV_X64_MSR_TIME_REF_COUNT:
986 case HV_X64_MSR_CRASH_CTL:
987 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
988 case HV_X64_MSR_RESET:
989 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
990 case HV_X64_MSR_TSC_EMULATION_CONTROL:
991 case HV_X64_MSR_TSC_EMULATION_STATUS:
992 case HV_X64_MSR_SYNDBG_OPTIONS:
993 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
1001 static int kvm_hv_msr_get_crash_data(struct kvm *kvm, u32 index, u64 *pdata)
1003 struct kvm_hv *hv = to_kvm_hv(kvm);
1004 size_t size = ARRAY_SIZE(hv->hv_crash_param);
1006 if (WARN_ON_ONCE(index >= size))
1009 *pdata = hv->hv_crash_param[array_index_nospec(index, size)];
1013 static int kvm_hv_msr_get_crash_ctl(struct kvm *kvm, u64 *pdata)
1015 struct kvm_hv *hv = to_kvm_hv(kvm);
1017 *pdata = hv->hv_crash_ctl;
1021 static int kvm_hv_msr_set_crash_ctl(struct kvm *kvm, u64 data)
1023 struct kvm_hv *hv = to_kvm_hv(kvm);
1025 hv->hv_crash_ctl = data & HV_CRASH_CTL_CRASH_NOTIFY;
1030 static int kvm_hv_msr_set_crash_data(struct kvm *kvm, u32 index, u64 data)
1032 struct kvm_hv *hv = to_kvm_hv(kvm);
1033 size_t size = ARRAY_SIZE(hv->hv_crash_param);
1035 if (WARN_ON_ONCE(index >= size))
1038 hv->hv_crash_param[array_index_nospec(index, size)] = data;
1043 * The kvmclock and Hyper-V TSC page use similar formulas, and converting
1044 * between them is possible:
1047 * nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32)
1051 * nsec/100 = ticks * scale / 2^64 + offset
1053 * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula.
1054 * By dividing the kvmclock formula by 100 and equating what's left we get:
1055 * ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
1056 * scale / 2^64 = tsc_to_system_mul * 2^(tsc_shift-32) / 100
1057 * scale = tsc_to_system_mul * 2^(32+tsc_shift) / 100
1059 * Now expand the kvmclock formula and divide by 100:
1060 * nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32)
1061 * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32)
1063 * nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
1064 * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100
1065 * + system_time / 100
1067 * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64:
1068 * nsec/100 = ticks * scale / 2^64
1069 * - tsc_timestamp * scale / 2^64
1070 * + system_time / 100
1072 * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out:
1073 * offset = system_time / 100 - tsc_timestamp * scale / 2^64
1075 * These two equivalencies are implemented in this function.
1077 static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock,
1078 struct ms_hyperv_tsc_page *tsc_ref)
1082 if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT))
1086 * check if scale would overflow, if so we use the time ref counter
1087 * tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64
1088 * tsc_to_system_mul / 100 >= 2^(32-tsc_shift)
1089 * tsc_to_system_mul >= 100 * 2^(32-tsc_shift)
1091 max_mul = 100ull << (32 - hv_clock->tsc_shift);
1092 if (hv_clock->tsc_to_system_mul >= max_mul)
1096 * Otherwise compute the scale and offset according to the formulas
1099 tsc_ref->tsc_scale =
1100 mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift),
1101 hv_clock->tsc_to_system_mul,
1104 tsc_ref->tsc_offset = hv_clock->system_time;
1105 do_div(tsc_ref->tsc_offset, 100);
1106 tsc_ref->tsc_offset -=
1107 mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64);
1112 * Don't touch TSC page values if the guest has opted for TSC emulation after
1113 * migration. KVM doesn't fully support reenlightenment notifications and TSC
1114 * access emulation and Hyper-V is known to expect the values in TSC page to
1115 * stay constant before TSC access emulation is disabled from guest side
1116 * (HV_X64_MSR_TSC_EMULATION_STATUS). KVM userspace is expected to preserve TSC
1117 * frequency and guest visible TSC value across migration (and prevent it when
1118 * TSC scaling is unsupported).
1120 static inline bool tsc_page_update_unsafe(struct kvm_hv *hv)
1122 return (hv->hv_tsc_page_status != HV_TSC_PAGE_GUEST_CHANGED) &&
1123 hv->hv_tsc_emulation_control;
1126 void kvm_hv_setup_tsc_page(struct kvm *kvm,
1127 struct pvclock_vcpu_time_info *hv_clock)
1129 struct kvm_hv *hv = to_kvm_hv(kvm);
1133 BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence));
1134 BUILD_BUG_ON(offsetof(struct ms_hyperv_tsc_page, tsc_sequence) != 0);
1136 mutex_lock(&hv->hv_lock);
1138 if (hv->hv_tsc_page_status == HV_TSC_PAGE_BROKEN ||
1139 hv->hv_tsc_page_status == HV_TSC_PAGE_SET ||
1140 hv->hv_tsc_page_status == HV_TSC_PAGE_UNSET)
1143 if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
1146 gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
1148 * Because the TSC parameters only vary when there is a
1149 * change in the master clock, do not bother with caching.
1151 if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn),
1152 &tsc_seq, sizeof(tsc_seq))))
1155 if (tsc_seq && tsc_page_update_unsafe(hv)) {
1156 if (kvm_read_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
1159 hv->hv_tsc_page_status = HV_TSC_PAGE_SET;
1164 * While we're computing and writing the parameters, force the
1165 * guest to use the time reference count MSR.
1167 hv->tsc_ref.tsc_sequence = 0;
1168 if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
1169 &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
1172 if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref))
1175 /* Ensure sequence is zero before writing the rest of the struct. */
1177 if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
1181 * Now switch to the TSC page mechanism by writing the sequence.
1184 if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0)
1187 /* Write the struct entirely before the non-zero sequence. */
1190 hv->tsc_ref.tsc_sequence = tsc_seq;
1191 if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
1192 &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
1195 hv->hv_tsc_page_status = HV_TSC_PAGE_SET;
1199 hv->hv_tsc_page_status = HV_TSC_PAGE_BROKEN;
1201 mutex_unlock(&hv->hv_lock);
1204 void kvm_hv_request_tsc_page_update(struct kvm *kvm)
1206 struct kvm_hv *hv = to_kvm_hv(kvm);
1208 mutex_lock(&hv->hv_lock);
1210 if (hv->hv_tsc_page_status == HV_TSC_PAGE_SET &&
1211 !tsc_page_update_unsafe(hv))
1212 hv->hv_tsc_page_status = HV_TSC_PAGE_HOST_CHANGED;
1214 mutex_unlock(&hv->hv_lock);
1217 static bool hv_check_msr_access(struct kvm_vcpu_hv *hv_vcpu, u32 msr)
1219 if (!hv_vcpu->enforce_cpuid)
1223 case HV_X64_MSR_GUEST_OS_ID:
1224 case HV_X64_MSR_HYPERCALL:
1225 return hv_vcpu->cpuid_cache.features_eax &
1226 HV_MSR_HYPERCALL_AVAILABLE;
1227 case HV_X64_MSR_VP_RUNTIME:
1228 return hv_vcpu->cpuid_cache.features_eax &
1229 HV_MSR_VP_RUNTIME_AVAILABLE;
1230 case HV_X64_MSR_TIME_REF_COUNT:
1231 return hv_vcpu->cpuid_cache.features_eax &
1232 HV_MSR_TIME_REF_COUNT_AVAILABLE;
1233 case HV_X64_MSR_VP_INDEX:
1234 return hv_vcpu->cpuid_cache.features_eax &
1235 HV_MSR_VP_INDEX_AVAILABLE;
1236 case HV_X64_MSR_RESET:
1237 return hv_vcpu->cpuid_cache.features_eax &
1238 HV_MSR_RESET_AVAILABLE;
1239 case HV_X64_MSR_REFERENCE_TSC:
1240 return hv_vcpu->cpuid_cache.features_eax &
1241 HV_MSR_REFERENCE_TSC_AVAILABLE;
1242 case HV_X64_MSR_SCONTROL:
1243 case HV_X64_MSR_SVERSION:
1244 case HV_X64_MSR_SIEFP:
1245 case HV_X64_MSR_SIMP:
1246 case HV_X64_MSR_EOM:
1247 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
1248 return hv_vcpu->cpuid_cache.features_eax &
1249 HV_MSR_SYNIC_AVAILABLE;
1250 case HV_X64_MSR_STIMER0_CONFIG:
1251 case HV_X64_MSR_STIMER1_CONFIG:
1252 case HV_X64_MSR_STIMER2_CONFIG:
1253 case HV_X64_MSR_STIMER3_CONFIG:
1254 case HV_X64_MSR_STIMER0_COUNT:
1255 case HV_X64_MSR_STIMER1_COUNT:
1256 case HV_X64_MSR_STIMER2_COUNT:
1257 case HV_X64_MSR_STIMER3_COUNT:
1258 return hv_vcpu->cpuid_cache.features_eax &
1259 HV_MSR_SYNTIMER_AVAILABLE;
1260 case HV_X64_MSR_EOI:
1261 case HV_X64_MSR_ICR:
1262 case HV_X64_MSR_TPR:
1263 case HV_X64_MSR_VP_ASSIST_PAGE:
1264 return hv_vcpu->cpuid_cache.features_eax &
1265 HV_MSR_APIC_ACCESS_AVAILABLE;
1267 case HV_X64_MSR_TSC_FREQUENCY:
1268 case HV_X64_MSR_APIC_FREQUENCY:
1269 return hv_vcpu->cpuid_cache.features_eax &
1270 HV_ACCESS_FREQUENCY_MSRS;
1271 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1272 case HV_X64_MSR_TSC_EMULATION_CONTROL:
1273 case HV_X64_MSR_TSC_EMULATION_STATUS:
1274 return hv_vcpu->cpuid_cache.features_eax &
1275 HV_ACCESS_REENLIGHTENMENT;
1276 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1277 case HV_X64_MSR_CRASH_CTL:
1278 return hv_vcpu->cpuid_cache.features_edx &
1279 HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE;
1280 case HV_X64_MSR_SYNDBG_OPTIONS:
1281 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
1282 return hv_vcpu->cpuid_cache.features_edx &
1283 HV_FEATURE_DEBUG_MSRS_AVAILABLE;
1291 static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
1294 struct kvm *kvm = vcpu->kvm;
1295 struct kvm_hv *hv = to_kvm_hv(kvm);
1297 if (unlikely(!host && !hv_check_msr_access(to_hv_vcpu(vcpu), msr)))
1301 case HV_X64_MSR_GUEST_OS_ID:
1302 hv->hv_guest_os_id = data;
1303 /* setting guest os id to zero disables hypercall page */
1304 if (!hv->hv_guest_os_id)
1305 hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
1307 case HV_X64_MSR_HYPERCALL: {
1312 /* if guest os id is not set hypercall should remain disabled */
1313 if (!hv->hv_guest_os_id)
1315 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
1316 hv->hv_hypercall = data;
1321 * If Xen and Hyper-V hypercalls are both enabled, disambiguate
1322 * the same way Xen itself does, by setting the bit 31 of EAX
1323 * which is RsvdZ in the 32-bit Hyper-V hypercall ABI and just
1324 * going to be clobbered on 64-bit.
1326 if (kvm_xen_hypercall_enabled(kvm)) {
1327 /* orl $0x80000000, %eax */
1328 instructions[i++] = 0x0d;
1329 instructions[i++] = 0x00;
1330 instructions[i++] = 0x00;
1331 instructions[i++] = 0x00;
1332 instructions[i++] = 0x80;
1335 /* vmcall/vmmcall */
1336 static_call(kvm_x86_patch_hypercall)(vcpu, instructions + i);
1340 ((unsigned char *)instructions)[i++] = 0xc3;
1342 addr = data & HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK;
1343 if (kvm_vcpu_write_guest(vcpu, addr, instructions, i))
1345 hv->hv_hypercall = data;
1348 case HV_X64_MSR_REFERENCE_TSC:
1349 hv->hv_tsc_page = data;
1350 if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE) {
1352 hv->hv_tsc_page_status = HV_TSC_PAGE_GUEST_CHANGED;
1354 hv->hv_tsc_page_status = HV_TSC_PAGE_HOST_CHANGED;
1355 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1357 hv->hv_tsc_page_status = HV_TSC_PAGE_UNSET;
1360 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1361 return kvm_hv_msr_set_crash_data(kvm,
1362 msr - HV_X64_MSR_CRASH_P0,
1364 case HV_X64_MSR_CRASH_CTL:
1366 return kvm_hv_msr_set_crash_ctl(kvm, data);
1368 if (data & HV_CRASH_CTL_CRASH_NOTIFY) {
1369 vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
1370 hv->hv_crash_param[0],
1371 hv->hv_crash_param[1],
1372 hv->hv_crash_param[2],
1373 hv->hv_crash_param[3],
1374 hv->hv_crash_param[4]);
1376 /* Send notification about crash to user space */
1377 kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
1380 case HV_X64_MSR_RESET:
1382 vcpu_debug(vcpu, "hyper-v reset requested\n");
1383 kvm_make_request(KVM_REQ_HV_RESET, vcpu);
1386 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1387 hv->hv_reenlightenment_control = data;
1389 case HV_X64_MSR_TSC_EMULATION_CONTROL:
1390 hv->hv_tsc_emulation_control = data;
1392 case HV_X64_MSR_TSC_EMULATION_STATUS:
1396 hv->hv_tsc_emulation_status = data;
1398 case HV_X64_MSR_TIME_REF_COUNT:
1399 /* read-only, but still ignore it if host-initiated */
1403 case HV_X64_MSR_SYNDBG_OPTIONS:
1404 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
1405 return syndbg_set_msr(vcpu, msr, data, host);
1407 vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n",
1414 /* Calculate cpu time spent by current task in 100ns units */
1415 static u64 current_task_runtime_100ns(void)
1419 task_cputime_adjusted(current, &utime, &stime);
1421 return div_u64(utime + stime, 100);
1424 static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
1426 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
1428 if (unlikely(!host && !hv_check_msr_access(hv_vcpu, msr)))
1432 case HV_X64_MSR_VP_INDEX: {
1433 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
1434 u32 new_vp_index = (u32)data;
1436 if (!host || new_vp_index >= KVM_MAX_VCPUS)
1439 if (new_vp_index == hv_vcpu->vp_index)
1443 * The VP index is initialized to vcpu_index by
1444 * kvm_hv_vcpu_postcreate so they initially match. Now the
1445 * VP index is changing, adjust num_mismatched_vp_indexes if
1446 * it now matches or no longer matches vcpu_idx.
1448 if (hv_vcpu->vp_index == vcpu->vcpu_idx)
1449 atomic_inc(&hv->num_mismatched_vp_indexes);
1450 else if (new_vp_index == vcpu->vcpu_idx)
1451 atomic_dec(&hv->num_mismatched_vp_indexes);
1453 hv_vcpu->vp_index = new_vp_index;
1456 case HV_X64_MSR_VP_ASSIST_PAGE: {
1460 if (!(data & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE)) {
1461 hv_vcpu->hv_vapic = data;
1462 if (kvm_lapic_set_pv_eoi(vcpu, 0, 0))
1466 gfn = data >> HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT;
1467 addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
1468 if (kvm_is_error_hva(addr))
1472 * Clear apic_assist portion of struct hv_vp_assist_page
1473 * only, there can be valuable data in the rest which needs
1474 * to be preserved e.g. on migration.
1476 if (__put_user(0, (u32 __user *)addr))
1478 hv_vcpu->hv_vapic = data;
1479 kvm_vcpu_mark_page_dirty(vcpu, gfn);
1480 if (kvm_lapic_set_pv_eoi(vcpu,
1481 gfn_to_gpa(gfn) | KVM_MSR_ENABLED,
1482 sizeof(struct hv_vp_assist_page)))
1486 case HV_X64_MSR_EOI:
1487 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1488 case HV_X64_MSR_ICR:
1489 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1490 case HV_X64_MSR_TPR:
1491 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
1492 case HV_X64_MSR_VP_RUNTIME:
1495 hv_vcpu->runtime_offset = data - current_task_runtime_100ns();
1497 case HV_X64_MSR_SCONTROL:
1498 case HV_X64_MSR_SVERSION:
1499 case HV_X64_MSR_SIEFP:
1500 case HV_X64_MSR_SIMP:
1501 case HV_X64_MSR_EOM:
1502 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
1503 return synic_set_msr(to_hv_synic(vcpu), msr, data, host);
1504 case HV_X64_MSR_STIMER0_CONFIG:
1505 case HV_X64_MSR_STIMER1_CONFIG:
1506 case HV_X64_MSR_STIMER2_CONFIG:
1507 case HV_X64_MSR_STIMER3_CONFIG: {
1508 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
1510 return stimer_set_config(to_hv_stimer(vcpu, timer_index),
1513 case HV_X64_MSR_STIMER0_COUNT:
1514 case HV_X64_MSR_STIMER1_COUNT:
1515 case HV_X64_MSR_STIMER2_COUNT:
1516 case HV_X64_MSR_STIMER3_COUNT: {
1517 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
1519 return stimer_set_count(to_hv_stimer(vcpu, timer_index),
1522 case HV_X64_MSR_TSC_FREQUENCY:
1523 case HV_X64_MSR_APIC_FREQUENCY:
1524 /* read-only, but still ignore it if host-initiated */
1529 vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n",
1537 static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,
1541 struct kvm *kvm = vcpu->kvm;
1542 struct kvm_hv *hv = to_kvm_hv(kvm);
1544 if (unlikely(!host && !hv_check_msr_access(to_hv_vcpu(vcpu), msr)))
1548 case HV_X64_MSR_GUEST_OS_ID:
1549 data = hv->hv_guest_os_id;
1551 case HV_X64_MSR_HYPERCALL:
1552 data = hv->hv_hypercall;
1554 case HV_X64_MSR_TIME_REF_COUNT:
1555 data = get_time_ref_counter(kvm);
1557 case HV_X64_MSR_REFERENCE_TSC:
1558 data = hv->hv_tsc_page;
1560 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1561 return kvm_hv_msr_get_crash_data(kvm,
1562 msr - HV_X64_MSR_CRASH_P0,
1564 case HV_X64_MSR_CRASH_CTL:
1565 return kvm_hv_msr_get_crash_ctl(kvm, pdata);
1566 case HV_X64_MSR_RESET:
1569 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1570 data = hv->hv_reenlightenment_control;
1572 case HV_X64_MSR_TSC_EMULATION_CONTROL:
1573 data = hv->hv_tsc_emulation_control;
1575 case HV_X64_MSR_TSC_EMULATION_STATUS:
1576 data = hv->hv_tsc_emulation_status;
1578 case HV_X64_MSR_SYNDBG_OPTIONS:
1579 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
1580 return syndbg_get_msr(vcpu, msr, pdata, host);
1582 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1590 static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,
1594 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
1596 if (unlikely(!host && !hv_check_msr_access(hv_vcpu, msr)))
1600 case HV_X64_MSR_VP_INDEX:
1601 data = hv_vcpu->vp_index;
1603 case HV_X64_MSR_EOI:
1604 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1605 case HV_X64_MSR_ICR:
1606 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1607 case HV_X64_MSR_TPR:
1608 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
1609 case HV_X64_MSR_VP_ASSIST_PAGE:
1610 data = hv_vcpu->hv_vapic;
1612 case HV_X64_MSR_VP_RUNTIME:
1613 data = current_task_runtime_100ns() + hv_vcpu->runtime_offset;
1615 case HV_X64_MSR_SCONTROL:
1616 case HV_X64_MSR_SVERSION:
1617 case HV_X64_MSR_SIEFP:
1618 case HV_X64_MSR_SIMP:
1619 case HV_X64_MSR_EOM:
1620 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
1621 return synic_get_msr(to_hv_synic(vcpu), msr, pdata, host);
1622 case HV_X64_MSR_STIMER0_CONFIG:
1623 case HV_X64_MSR_STIMER1_CONFIG:
1624 case HV_X64_MSR_STIMER2_CONFIG:
1625 case HV_X64_MSR_STIMER3_CONFIG: {
1626 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
1628 return stimer_get_config(to_hv_stimer(vcpu, timer_index),
1631 case HV_X64_MSR_STIMER0_COUNT:
1632 case HV_X64_MSR_STIMER1_COUNT:
1633 case HV_X64_MSR_STIMER2_COUNT:
1634 case HV_X64_MSR_STIMER3_COUNT: {
1635 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
1637 return stimer_get_count(to_hv_stimer(vcpu, timer_index),
1640 case HV_X64_MSR_TSC_FREQUENCY:
1641 data = (u64)vcpu->arch.virtual_tsc_khz * 1000;
1643 case HV_X64_MSR_APIC_FREQUENCY:
1644 data = APIC_BUS_FREQUENCY;
1647 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1654 int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
1656 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
1658 if (!host && !vcpu->arch.hyperv_enabled)
1661 if (kvm_hv_vcpu_init(vcpu))
1664 if (kvm_hv_msr_partition_wide(msr)) {
1667 mutex_lock(&hv->hv_lock);
1668 r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
1669 mutex_unlock(&hv->hv_lock);
1672 return kvm_hv_set_msr(vcpu, msr, data, host);
1675 int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
1677 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
1679 if (!host && !vcpu->arch.hyperv_enabled)
1682 if (kvm_hv_vcpu_init(vcpu))
1685 if (kvm_hv_msr_partition_wide(msr)) {
1688 mutex_lock(&hv->hv_lock);
1689 r = kvm_hv_get_msr_pw(vcpu, msr, pdata, host);
1690 mutex_unlock(&hv->hv_lock);
1693 return kvm_hv_get_msr(vcpu, msr, pdata, host);
1696 static void sparse_set_to_vcpu_mask(struct kvm *kvm, u64 *sparse_banks,
1697 u64 valid_bank_mask, unsigned long *vcpu_mask)
1699 struct kvm_hv *hv = to_kvm_hv(kvm);
1700 bool has_mismatch = atomic_read(&hv->num_mismatched_vp_indexes);
1701 u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS];
1702 struct kvm_vcpu *vcpu;
1703 int bank, sbank = 0;
1707 BUILD_BUG_ON(sizeof(vp_bitmap) >
1708 sizeof(*vcpu_mask) * BITS_TO_LONGS(KVM_MAX_VCPUS));
1711 * If vp_index == vcpu_idx for all vCPUs, fill vcpu_mask directly, else
1712 * fill a temporary buffer and manually test each vCPU's VP index.
1714 if (likely(!has_mismatch))
1715 bitmap = (u64 *)vcpu_mask;
1720 * Each set of 64 VPs is packed into sparse_banks, with valid_bank_mask
1721 * having a '1' for each bank that exists in sparse_banks. Sets must
1722 * be in ascending order, i.e. bank0..bankN.
1724 memset(bitmap, 0, sizeof(vp_bitmap));
1725 for_each_set_bit(bank, (unsigned long *)&valid_bank_mask,
1726 KVM_HV_MAX_SPARSE_VCPU_SET_BITS)
1727 bitmap[bank] = sparse_banks[sbank++];
1729 if (likely(!has_mismatch))
1732 bitmap_zero(vcpu_mask, KVM_MAX_VCPUS);
1733 kvm_for_each_vcpu(i, vcpu, kvm) {
1734 if (test_bit(kvm_hv_get_vpindex(vcpu), (unsigned long *)vp_bitmap))
1735 __set_bit(i, vcpu_mask);
1739 struct kvm_hv_hcall {
1749 sse128_t xmm[HV_HYPERCALL_MAX_XMM_REGISTERS];
1752 static u64 kvm_get_sparse_vp_set(struct kvm *kvm, struct kvm_hv_hcall *hc,
1753 int consumed_xmm_halves,
1754 u64 *sparse_banks, gpa_t offset)
1759 if (hc->var_cnt > 64)
1762 /* Ignore banks that cannot possibly contain a legal VP index. */
1763 var_cnt = min_t(u16, hc->var_cnt, KVM_HV_MAX_SPARSE_VCPU_SET_BITS);
1767 * Each XMM holds two sparse banks, but do not count halves that
1768 * have already been consumed for hypercall parameters.
1770 if (hc->var_cnt > 2 * HV_HYPERCALL_MAX_XMM_REGISTERS - consumed_xmm_halves)
1771 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1772 for (i = 0; i < var_cnt; i++) {
1773 int j = i + consumed_xmm_halves;
1775 sparse_banks[i] = sse128_hi(hc->xmm[j / 2]);
1777 sparse_banks[i] = sse128_lo(hc->xmm[j / 2]);
1782 return kvm_read_guest(kvm, hc->ingpa + offset, sparse_banks,
1783 var_cnt * sizeof(*sparse_banks));
1786 static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
1788 struct kvm *kvm = vcpu->kvm;
1789 struct hv_tlb_flush_ex flush_ex;
1790 struct hv_tlb_flush flush;
1791 DECLARE_BITMAP(vcpu_mask, KVM_MAX_VCPUS);
1792 u64 valid_bank_mask;
1793 u64 sparse_banks[KVM_HV_MAX_SPARSE_VCPU_SET_BITS];
1797 * The Hyper-V TLFS doesn't allow more than 64 sparse banks, e.g. the
1798 * valid mask is a u64. Fail the build if KVM's max allowed number of
1799 * vCPUs (>4096) would exceed this limit, KVM will additional changes
1800 * for Hyper-V support to avoid setting the guest up to fail.
1802 BUILD_BUG_ON(KVM_HV_MAX_SPARSE_VCPU_SET_BITS > 64);
1804 if (hc->code == HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST ||
1805 hc->code == HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE) {
1807 flush.address_space = hc->ingpa;
1808 flush.flags = hc->outgpa;
1809 flush.processor_mask = sse128_lo(hc->xmm[0]);
1811 if (unlikely(kvm_read_guest(kvm, hc->ingpa,
1812 &flush, sizeof(flush))))
1813 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1816 trace_kvm_hv_flush_tlb(flush.processor_mask,
1817 flush.address_space, flush.flags);
1819 valid_bank_mask = BIT_ULL(0);
1820 sparse_banks[0] = flush.processor_mask;
1823 * Work around possible WS2012 bug: it sends hypercalls
1824 * with processor_mask = 0x0 and HV_FLUSH_ALL_PROCESSORS clear,
1825 * while also expecting us to flush something and crashing if
1826 * we don't. Let's treat processor_mask == 0 same as
1827 * HV_FLUSH_ALL_PROCESSORS.
1829 all_cpus = (flush.flags & HV_FLUSH_ALL_PROCESSORS) ||
1830 flush.processor_mask == 0;
1833 flush_ex.address_space = hc->ingpa;
1834 flush_ex.flags = hc->outgpa;
1835 memcpy(&flush_ex.hv_vp_set,
1836 &hc->xmm[0], sizeof(hc->xmm[0]));
1838 if (unlikely(kvm_read_guest(kvm, hc->ingpa, &flush_ex,
1840 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1843 trace_kvm_hv_flush_tlb_ex(flush_ex.hv_vp_set.valid_bank_mask,
1844 flush_ex.hv_vp_set.format,
1845 flush_ex.address_space,
1848 valid_bank_mask = flush_ex.hv_vp_set.valid_bank_mask;
1849 all_cpus = flush_ex.hv_vp_set.format !=
1850 HV_GENERIC_SET_SPARSE_4K;
1852 if (hc->var_cnt != hweight64(valid_bank_mask))
1853 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1861 if (kvm_get_sparse_vp_set(kvm, hc, 2, sparse_banks,
1862 offsetof(struct hv_tlb_flush_ex,
1863 hv_vp_set.bank_contents)))
1864 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1869 * vcpu->arch.cr3 may not be up-to-date for running vCPUs so we can't
1870 * analyze it here, flush TLB regardless of the specified address space.
1873 kvm_make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH_GUEST);
1875 sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask, vcpu_mask);
1877 kvm_make_vcpus_request_mask(kvm, KVM_REQ_TLB_FLUSH_GUEST, vcpu_mask);
1881 /* We always do full TLB flush, set 'Reps completed' = 'Rep Count' */
1882 return (u64)HV_STATUS_SUCCESS |
1883 ((u64)hc->rep_cnt << HV_HYPERCALL_REP_COMP_OFFSET);
1886 static void kvm_send_ipi_to_many(struct kvm *kvm, u32 vector,
1887 unsigned long *vcpu_bitmap)
1889 struct kvm_lapic_irq irq = {
1890 .delivery_mode = APIC_DM_FIXED,
1893 struct kvm_vcpu *vcpu;
1896 kvm_for_each_vcpu(i, vcpu, kvm) {
1897 if (vcpu_bitmap && !test_bit(i, vcpu_bitmap))
1900 /* We fail only when APIC is disabled */
1901 kvm_apic_set_irq(vcpu, &irq, NULL);
1905 static u64 kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
1907 struct kvm *kvm = vcpu->kvm;
1908 struct hv_send_ipi_ex send_ipi_ex;
1909 struct hv_send_ipi send_ipi;
1910 DECLARE_BITMAP(vcpu_mask, KVM_MAX_VCPUS);
1911 u64 valid_bank_mask;
1912 u64 sparse_banks[KVM_HV_MAX_SPARSE_VCPU_SET_BITS];
1916 if (hc->code == HVCALL_SEND_IPI) {
1918 if (unlikely(kvm_read_guest(kvm, hc->ingpa, &send_ipi,
1920 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1921 sparse_banks[0] = send_ipi.cpu_mask;
1922 vector = send_ipi.vector;
1924 /* 'reserved' part of hv_send_ipi should be 0 */
1925 if (unlikely(hc->ingpa >> 32 != 0))
1926 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1927 sparse_banks[0] = hc->outgpa;
1928 vector = (u32)hc->ingpa;
1931 valid_bank_mask = BIT_ULL(0);
1933 trace_kvm_hv_send_ipi(vector, sparse_banks[0]);
1936 if (unlikely(kvm_read_guest(kvm, hc->ingpa, &send_ipi_ex,
1937 sizeof(send_ipi_ex))))
1938 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1940 send_ipi_ex.vector = (u32)hc->ingpa;
1941 send_ipi_ex.vp_set.format = hc->outgpa;
1942 send_ipi_ex.vp_set.valid_bank_mask = sse128_lo(hc->xmm[0]);
1945 trace_kvm_hv_send_ipi_ex(send_ipi_ex.vector,
1946 send_ipi_ex.vp_set.format,
1947 send_ipi_ex.vp_set.valid_bank_mask);
1949 vector = send_ipi_ex.vector;
1950 valid_bank_mask = send_ipi_ex.vp_set.valid_bank_mask;
1951 all_cpus = send_ipi_ex.vp_set.format == HV_GENERIC_SET_ALL;
1953 if (hc->var_cnt != hweight64(valid_bank_mask))
1954 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1957 goto check_and_send_ipi;
1962 if (kvm_get_sparse_vp_set(kvm, hc, 1, sparse_banks,
1963 offsetof(struct hv_send_ipi_ex,
1964 vp_set.bank_contents)))
1965 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1969 if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR))
1970 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1973 kvm_send_ipi_to_many(kvm, vector, NULL);
1975 sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask, vcpu_mask);
1977 kvm_send_ipi_to_many(kvm, vector, vcpu_mask);
1981 return HV_STATUS_SUCCESS;
1984 void kvm_hv_set_cpuid(struct kvm_vcpu *vcpu, bool hyperv_enabled)
1986 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
1987 struct kvm_cpuid_entry2 *entry;
1989 vcpu->arch.hyperv_enabled = hyperv_enabled;
1993 * KVM should have already allocated kvm_vcpu_hv if Hyper-V is
1996 WARN_ON_ONCE(vcpu->arch.hyperv_enabled);
2000 memset(&hv_vcpu->cpuid_cache, 0, sizeof(hv_vcpu->cpuid_cache));
2002 if (!vcpu->arch.hyperv_enabled)
2005 entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_FEATURES);
2007 hv_vcpu->cpuid_cache.features_eax = entry->eax;
2008 hv_vcpu->cpuid_cache.features_ebx = entry->ebx;
2009 hv_vcpu->cpuid_cache.features_edx = entry->edx;
2012 entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_ENLIGHTMENT_INFO);
2014 hv_vcpu->cpuid_cache.enlightenments_eax = entry->eax;
2015 hv_vcpu->cpuid_cache.enlightenments_ebx = entry->ebx;
2018 entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES);
2020 hv_vcpu->cpuid_cache.syndbg_cap_eax = entry->eax;
2022 entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_NESTED_FEATURES);
2024 hv_vcpu->cpuid_cache.nested_eax = entry->eax;
2025 hv_vcpu->cpuid_cache.nested_ebx = entry->ebx;
2029 int kvm_hv_set_enforce_cpuid(struct kvm_vcpu *vcpu, bool enforce)
2031 struct kvm_vcpu_hv *hv_vcpu;
2034 if (!to_hv_vcpu(vcpu)) {
2036 ret = kvm_hv_vcpu_init(vcpu);
2044 hv_vcpu = to_hv_vcpu(vcpu);
2045 hv_vcpu->enforce_cpuid = enforce;
2050 static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
2054 longmode = is_64_bit_hypercall(vcpu);
2056 kvm_rax_write(vcpu, result);
2058 kvm_rdx_write(vcpu, result >> 32);
2059 kvm_rax_write(vcpu, result & 0xffffffff);
2063 static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result)
2065 trace_kvm_hv_hypercall_done(result);
2066 kvm_hv_hypercall_set_result(vcpu, result);
2067 ++vcpu->stat.hypercalls;
2068 return kvm_skip_emulated_instruction(vcpu);
2071 static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
2073 return kvm_hv_hypercall_complete(vcpu, vcpu->run->hyperv.u.hcall.result);
2076 static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
2078 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
2079 struct eventfd_ctx *eventfd;
2081 if (unlikely(!hc->fast)) {
2083 gpa_t gpa = hc->ingpa;
2085 if ((gpa & (__alignof__(hc->ingpa) - 1)) ||
2086 offset_in_page(gpa) + sizeof(hc->ingpa) > PAGE_SIZE)
2087 return HV_STATUS_INVALID_ALIGNMENT;
2089 ret = kvm_vcpu_read_guest(vcpu, gpa,
2090 &hc->ingpa, sizeof(hc->ingpa));
2092 return HV_STATUS_INVALID_ALIGNMENT;
2096 * Per spec, bits 32-47 contain the extra "flag number". However, we
2097 * have no use for it, and in all known usecases it is zero, so just
2098 * report lookup failure if it isn't.
2100 if (hc->ingpa & 0xffff00000000ULL)
2101 return HV_STATUS_INVALID_PORT_ID;
2102 /* remaining bits are reserved-zero */
2103 if (hc->ingpa & ~KVM_HYPERV_CONN_ID_MASK)
2104 return HV_STATUS_INVALID_HYPERCALL_INPUT;
2106 /* the eventfd is protected by vcpu->kvm->srcu, but conn_to_evt isn't */
2108 eventfd = idr_find(&hv->conn_to_evt, hc->ingpa);
2111 return HV_STATUS_INVALID_PORT_ID;
2113 eventfd_signal(eventfd, 1);
2114 return HV_STATUS_SUCCESS;
2117 static bool is_xmm_fast_hypercall(struct kvm_hv_hcall *hc)
2120 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST:
2121 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE:
2122 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX:
2123 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX:
2124 case HVCALL_SEND_IPI_EX:
2131 static void kvm_hv_hypercall_read_xmm(struct kvm_hv_hcall *hc)
2136 for (reg = 0; reg < HV_HYPERCALL_MAX_XMM_REGISTERS; reg++)
2137 _kvm_read_sse_reg(reg, &hc->xmm[reg]);
2141 static bool hv_check_hypercall_access(struct kvm_vcpu_hv *hv_vcpu, u16 code)
2143 if (!hv_vcpu->enforce_cpuid)
2147 case HVCALL_NOTIFY_LONG_SPIN_WAIT:
2148 return hv_vcpu->cpuid_cache.enlightenments_ebx &&
2149 hv_vcpu->cpuid_cache.enlightenments_ebx != U32_MAX;
2150 case HVCALL_POST_MESSAGE:
2151 return hv_vcpu->cpuid_cache.features_ebx & HV_POST_MESSAGES;
2152 case HVCALL_SIGNAL_EVENT:
2153 return hv_vcpu->cpuid_cache.features_ebx & HV_SIGNAL_EVENTS;
2154 case HVCALL_POST_DEBUG_DATA:
2155 case HVCALL_RETRIEVE_DEBUG_DATA:
2156 case HVCALL_RESET_DEBUG_SESSION:
2158 * Return 'true' when SynDBG is disabled so the resulting code
2159 * will be HV_STATUS_INVALID_HYPERCALL_CODE.
2161 return !kvm_hv_is_syndbg_enabled(hv_vcpu->vcpu) ||
2162 hv_vcpu->cpuid_cache.features_ebx & HV_DEBUGGING;
2163 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX:
2164 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX:
2165 if (!(hv_vcpu->cpuid_cache.enlightenments_eax &
2166 HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED))
2169 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST:
2170 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE:
2171 return hv_vcpu->cpuid_cache.enlightenments_eax &
2172 HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED;
2173 case HVCALL_SEND_IPI_EX:
2174 if (!(hv_vcpu->cpuid_cache.enlightenments_eax &
2175 HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED))
2178 case HVCALL_SEND_IPI:
2179 return hv_vcpu->cpuid_cache.enlightenments_eax &
2180 HV_X64_CLUSTER_IPI_RECOMMENDED;
2188 int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
2190 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
2191 struct kvm_hv_hcall hc;
2192 u64 ret = HV_STATUS_SUCCESS;
2195 * hypercall generates UD from non zero cpl and real mode
2198 if (static_call(kvm_x86_get_cpl)(vcpu) != 0 || !is_protmode(vcpu)) {
2199 kvm_queue_exception(vcpu, UD_VECTOR);
2203 #ifdef CONFIG_X86_64
2204 if (is_64_bit_hypercall(vcpu)) {
2205 hc.param = kvm_rcx_read(vcpu);
2206 hc.ingpa = kvm_rdx_read(vcpu);
2207 hc.outgpa = kvm_r8_read(vcpu);
2211 hc.param = ((u64)kvm_rdx_read(vcpu) << 32) |
2212 (kvm_rax_read(vcpu) & 0xffffffff);
2213 hc.ingpa = ((u64)kvm_rbx_read(vcpu) << 32) |
2214 (kvm_rcx_read(vcpu) & 0xffffffff);
2215 hc.outgpa = ((u64)kvm_rdi_read(vcpu) << 32) |
2216 (kvm_rsi_read(vcpu) & 0xffffffff);
2219 hc.code = hc.param & 0xffff;
2220 hc.var_cnt = (hc.param & HV_HYPERCALL_VARHEAD_MASK) >> HV_HYPERCALL_VARHEAD_OFFSET;
2221 hc.fast = !!(hc.param & HV_HYPERCALL_FAST_BIT);
2222 hc.rep_cnt = (hc.param >> HV_HYPERCALL_REP_COMP_OFFSET) & 0xfff;
2223 hc.rep_idx = (hc.param >> HV_HYPERCALL_REP_START_OFFSET) & 0xfff;
2224 hc.rep = !!(hc.rep_cnt || hc.rep_idx);
2226 trace_kvm_hv_hypercall(hc.code, hc.fast, hc.var_cnt, hc.rep_cnt,
2227 hc.rep_idx, hc.ingpa, hc.outgpa);
2229 if (unlikely(!hv_check_hypercall_access(hv_vcpu, hc.code))) {
2230 ret = HV_STATUS_ACCESS_DENIED;
2231 goto hypercall_complete;
2234 if (unlikely(hc.param & HV_HYPERCALL_RSVD_MASK)) {
2235 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2236 goto hypercall_complete;
2239 if (hc.fast && is_xmm_fast_hypercall(&hc)) {
2240 if (unlikely(hv_vcpu->enforce_cpuid &&
2241 !(hv_vcpu->cpuid_cache.features_edx &
2242 HV_X64_HYPERCALL_XMM_INPUT_AVAILABLE))) {
2243 kvm_queue_exception(vcpu, UD_VECTOR);
2247 kvm_hv_hypercall_read_xmm(&hc);
2251 case HVCALL_NOTIFY_LONG_SPIN_WAIT:
2252 if (unlikely(hc.rep || hc.var_cnt)) {
2253 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2256 kvm_vcpu_on_spin(vcpu, true);
2258 case HVCALL_SIGNAL_EVENT:
2259 if (unlikely(hc.rep || hc.var_cnt)) {
2260 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2263 ret = kvm_hvcall_signal_event(vcpu, &hc);
2264 if (ret != HV_STATUS_INVALID_PORT_ID)
2266 fallthrough; /* maybe userspace knows this conn_id */
2267 case HVCALL_POST_MESSAGE:
2268 /* don't bother userspace if it has no way to handle it */
2269 if (unlikely(hc.rep || hc.var_cnt || !to_hv_synic(vcpu)->active)) {
2270 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2273 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
2274 vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
2275 vcpu->run->hyperv.u.hcall.input = hc.param;
2276 vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa;
2277 vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa;
2278 vcpu->arch.complete_userspace_io =
2279 kvm_hv_hypercall_complete_userspace;
2281 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST:
2282 if (unlikely(hc.var_cnt)) {
2283 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2287 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX:
2288 if (unlikely(!hc.rep_cnt || hc.rep_idx)) {
2289 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2292 ret = kvm_hv_flush_tlb(vcpu, &hc);
2294 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE:
2295 if (unlikely(hc.var_cnt)) {
2296 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2300 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX:
2301 if (unlikely(hc.rep)) {
2302 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2305 ret = kvm_hv_flush_tlb(vcpu, &hc);
2307 case HVCALL_SEND_IPI:
2308 if (unlikely(hc.var_cnt)) {
2309 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2313 case HVCALL_SEND_IPI_EX:
2314 if (unlikely(hc.rep)) {
2315 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2318 ret = kvm_hv_send_ipi(vcpu, &hc);
2320 case HVCALL_POST_DEBUG_DATA:
2321 case HVCALL_RETRIEVE_DEBUG_DATA:
2322 if (unlikely(hc.fast)) {
2323 ret = HV_STATUS_INVALID_PARAMETER;
2327 case HVCALL_RESET_DEBUG_SESSION: {
2328 struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu);
2330 if (!kvm_hv_is_syndbg_enabled(vcpu)) {
2331 ret = HV_STATUS_INVALID_HYPERCALL_CODE;
2335 if (!(syndbg->options & HV_X64_SYNDBG_OPTION_USE_HCALLS)) {
2336 ret = HV_STATUS_OPERATION_DENIED;
2339 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
2340 vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
2341 vcpu->run->hyperv.u.hcall.input = hc.param;
2342 vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa;
2343 vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa;
2344 vcpu->arch.complete_userspace_io =
2345 kvm_hv_hypercall_complete_userspace;
2349 ret = HV_STATUS_INVALID_HYPERCALL_CODE;
2354 return kvm_hv_hypercall_complete(vcpu, ret);
2357 void kvm_hv_init_vm(struct kvm *kvm)
2359 struct kvm_hv *hv = to_kvm_hv(kvm);
2361 mutex_init(&hv->hv_lock);
2362 idr_init(&hv->conn_to_evt);
2365 void kvm_hv_destroy_vm(struct kvm *kvm)
2367 struct kvm_hv *hv = to_kvm_hv(kvm);
2368 struct eventfd_ctx *eventfd;
2371 idr_for_each_entry(&hv->conn_to_evt, eventfd, i)
2372 eventfd_ctx_put(eventfd);
2373 idr_destroy(&hv->conn_to_evt);
2376 static int kvm_hv_eventfd_assign(struct kvm *kvm, u32 conn_id, int fd)
2378 struct kvm_hv *hv = to_kvm_hv(kvm);
2379 struct eventfd_ctx *eventfd;
2382 eventfd = eventfd_ctx_fdget(fd);
2383 if (IS_ERR(eventfd))
2384 return PTR_ERR(eventfd);
2386 mutex_lock(&hv->hv_lock);
2387 ret = idr_alloc(&hv->conn_to_evt, eventfd, conn_id, conn_id + 1,
2388 GFP_KERNEL_ACCOUNT);
2389 mutex_unlock(&hv->hv_lock);
2396 eventfd_ctx_put(eventfd);
2400 static int kvm_hv_eventfd_deassign(struct kvm *kvm, u32 conn_id)
2402 struct kvm_hv *hv = to_kvm_hv(kvm);
2403 struct eventfd_ctx *eventfd;
2405 mutex_lock(&hv->hv_lock);
2406 eventfd = idr_remove(&hv->conn_to_evt, conn_id);
2407 mutex_unlock(&hv->hv_lock);
2412 synchronize_srcu(&kvm->srcu);
2413 eventfd_ctx_put(eventfd);
2417 int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args)
2419 if ((args->flags & ~KVM_HYPERV_EVENTFD_DEASSIGN) ||
2420 (args->conn_id & ~KVM_HYPERV_CONN_ID_MASK))
2423 if (args->flags == KVM_HYPERV_EVENTFD_DEASSIGN)
2424 return kvm_hv_eventfd_deassign(kvm, args->conn_id);
2425 return kvm_hv_eventfd_assign(kvm, args->conn_id, args->fd);
2428 int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,
2429 struct kvm_cpuid_entry2 __user *entries)
2431 uint16_t evmcs_ver = 0;
2432 struct kvm_cpuid_entry2 cpuid_entries[] = {
2433 { .function = HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS },
2434 { .function = HYPERV_CPUID_INTERFACE },
2435 { .function = HYPERV_CPUID_VERSION },
2436 { .function = HYPERV_CPUID_FEATURES },
2437 { .function = HYPERV_CPUID_ENLIGHTMENT_INFO },
2438 { .function = HYPERV_CPUID_IMPLEMENT_LIMITS },
2439 { .function = HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS },
2440 { .function = HYPERV_CPUID_SYNDBG_INTERFACE },
2441 { .function = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES },
2442 { .function = HYPERV_CPUID_NESTED_FEATURES },
2444 int i, nent = ARRAY_SIZE(cpuid_entries);
2446 if (kvm_x86_ops.nested_ops->get_evmcs_version)
2447 evmcs_ver = kvm_x86_ops.nested_ops->get_evmcs_version(vcpu);
2449 if (cpuid->nent < nent)
2452 if (cpuid->nent > nent)
2455 for (i = 0; i < nent; i++) {
2456 struct kvm_cpuid_entry2 *ent = &cpuid_entries[i];
2459 switch (ent->function) {
2460 case HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS:
2461 memcpy(signature, "Linux KVM Hv", 12);
2463 ent->eax = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES;
2464 ent->ebx = signature[0];
2465 ent->ecx = signature[1];
2466 ent->edx = signature[2];
2469 case HYPERV_CPUID_INTERFACE:
2470 ent->eax = HYPERV_CPUID_SIGNATURE_EAX;
2473 case HYPERV_CPUID_VERSION:
2475 * We implement some Hyper-V 2016 functions so let's use
2478 ent->eax = 0x00003839;
2479 ent->ebx = 0x000A0000;
2482 case HYPERV_CPUID_FEATURES:
2483 ent->eax |= HV_MSR_VP_RUNTIME_AVAILABLE;
2484 ent->eax |= HV_MSR_TIME_REF_COUNT_AVAILABLE;
2485 ent->eax |= HV_MSR_SYNIC_AVAILABLE;
2486 ent->eax |= HV_MSR_SYNTIMER_AVAILABLE;
2487 ent->eax |= HV_MSR_APIC_ACCESS_AVAILABLE;
2488 ent->eax |= HV_MSR_HYPERCALL_AVAILABLE;
2489 ent->eax |= HV_MSR_VP_INDEX_AVAILABLE;
2490 ent->eax |= HV_MSR_RESET_AVAILABLE;
2491 ent->eax |= HV_MSR_REFERENCE_TSC_AVAILABLE;
2492 ent->eax |= HV_ACCESS_FREQUENCY_MSRS;
2493 ent->eax |= HV_ACCESS_REENLIGHTENMENT;
2495 ent->ebx |= HV_POST_MESSAGES;
2496 ent->ebx |= HV_SIGNAL_EVENTS;
2498 ent->edx |= HV_X64_HYPERCALL_XMM_INPUT_AVAILABLE;
2499 ent->edx |= HV_FEATURE_FREQUENCY_MSRS_AVAILABLE;
2500 ent->edx |= HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE;
2502 ent->ebx |= HV_DEBUGGING;
2503 ent->edx |= HV_X64_GUEST_DEBUGGING_AVAILABLE;
2504 ent->edx |= HV_FEATURE_DEBUG_MSRS_AVAILABLE;
2507 * Direct Synthetic timers only make sense with in-kernel
2510 if (!vcpu || lapic_in_kernel(vcpu))
2511 ent->edx |= HV_STIMER_DIRECT_MODE_AVAILABLE;
2515 case HYPERV_CPUID_ENLIGHTMENT_INFO:
2516 ent->eax |= HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED;
2517 ent->eax |= HV_X64_APIC_ACCESS_RECOMMENDED;
2518 ent->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED;
2519 ent->eax |= HV_X64_CLUSTER_IPI_RECOMMENDED;
2520 ent->eax |= HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED;
2522 ent->eax |= HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
2523 if (!cpu_smt_possible())
2524 ent->eax |= HV_X64_NO_NONARCH_CORESHARING;
2526 ent->eax |= HV_DEPRECATING_AEOI_RECOMMENDED;
2528 * Default number of spinlock retry attempts, matches
2531 ent->ebx = 0x00000FFF;
2535 case HYPERV_CPUID_IMPLEMENT_LIMITS:
2536 /* Maximum number of virtual processors */
2537 ent->eax = KVM_MAX_VCPUS;
2539 * Maximum number of logical processors, matches
2546 case HYPERV_CPUID_NESTED_FEATURES:
2547 ent->eax = evmcs_ver;
2548 ent->eax |= HV_X64_NESTED_MSR_BITMAP;
2549 ent->ebx |= HV_X64_NESTED_EVMCS1_PERF_GLOBAL_CTRL;
2552 case HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS:
2553 memcpy(signature, "Linux KVM Hv", 12);
2556 ent->ebx = signature[0];
2557 ent->ecx = signature[1];
2558 ent->edx = signature[2];
2561 case HYPERV_CPUID_SYNDBG_INTERFACE:
2562 memcpy(signature, "VS#1\0\0\0\0\0\0\0\0", 12);
2563 ent->eax = signature[0];
2566 case HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES:
2567 ent->eax |= HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING;
2575 if (copy_to_user(entries, cpuid_entries,
2576 nent * sizeof(struct kvm_cpuid_entry2)))