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.
10 * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com>
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
15 * Amit Shah <amit.shah@qumranet.com>
16 * Ben-Ami Yassour <benami@il.ibm.com>
17 * Andrey Smetanin <asmetanin@virtuozzo.com>
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 <asm/apicdef.h>
31 #include <trace/events/kvm.h>
35 static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
37 return atomic64_read(&synic->sint[sint]);
40 static inline int synic_get_sint_vector(u64 sint_value)
42 if (sint_value & HV_SYNIC_SINT_MASKED)
44 return sint_value & HV_SYNIC_SINT_VECTOR_MASK;
47 static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic,
52 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
53 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
59 static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic,
65 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
66 sint_value = synic_read_sint(synic, i);
67 if (synic_get_sint_vector(sint_value) == vector &&
68 sint_value & HV_SYNIC_SINT_AUTO_EOI)
74 static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint, u64 data)
78 vector = data & HV_SYNIC_SINT_VECTOR_MASK;
82 * Guest may configure multiple SINTs to use the same vector, so
83 * we maintain a bitmap of vectors handled by synic, and a
84 * bitmap of vectors with auto-eoi behavior. The bitmaps are
85 * updated here, and atomically queried on fast paths.
88 atomic64_set(&synic->sint[sint], data);
90 if (synic_has_vector_connected(synic, vector))
91 __set_bit(vector, synic->vec_bitmap);
93 __clear_bit(vector, synic->vec_bitmap);
95 if (synic_has_vector_auto_eoi(synic, vector))
96 __set_bit(vector, synic->auto_eoi_bitmap);
98 __clear_bit(vector, synic->auto_eoi_bitmap);
100 /* Load SynIC vectors into EOI exit bitmap */
101 kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic));
105 static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vcpu_id)
107 struct kvm_vcpu *vcpu;
108 struct kvm_vcpu_hv_synic *synic;
110 if (vcpu_id >= atomic_read(&kvm->online_vcpus))
112 vcpu = kvm_get_vcpu(kvm, vcpu_id);
115 synic = vcpu_to_synic(vcpu);
116 return (synic->active) ? synic : NULL;
119 static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
121 struct kvm *kvm = vcpu->kvm;
124 vcpu_debug(vcpu, "Hyper-V SynIC acked sint %d\n", sint);
126 idx = srcu_read_lock(&kvm->irq_srcu);
127 gsi = atomic_read(&vcpu_to_synic(vcpu)->sint_to_gsi[sint]);
129 kvm_notify_acked_gsi(kvm, gsi);
130 srcu_read_unlock(&kvm->irq_srcu, idx);
133 static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
135 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
136 struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
138 hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
139 hv_vcpu->exit.u.synic.msr = msr;
140 hv_vcpu->exit.u.synic.control = synic->control;
141 hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
142 hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
144 kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
147 static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
148 u32 msr, u64 data, bool host)
150 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
156 vcpu_debug(vcpu, "Hyper-V SynIC set msr 0x%x 0x%llx host %d\n",
160 case HV_X64_MSR_SCONTROL:
161 synic->control = data;
163 synic_exit(synic, msr);
165 case HV_X64_MSR_SVERSION:
170 synic->version = data;
172 case HV_X64_MSR_SIEFP:
173 if (data & HV_SYNIC_SIEFP_ENABLE)
174 if (kvm_clear_guest(vcpu->kvm,
175 data & PAGE_MASK, PAGE_SIZE)) {
179 synic->evt_page = data;
181 synic_exit(synic, msr);
183 case HV_X64_MSR_SIMP:
184 if (data & HV_SYNIC_SIMP_ENABLE)
185 if (kvm_clear_guest(vcpu->kvm,
186 data & PAGE_MASK, PAGE_SIZE)) {
190 synic->msg_page = data;
192 synic_exit(synic, msr);
194 case HV_X64_MSR_EOM: {
197 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
198 kvm_hv_notify_acked_sint(vcpu, i);
201 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
202 ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data);
211 static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata)
220 case HV_X64_MSR_SCONTROL:
221 *pdata = synic->control;
223 case HV_X64_MSR_SVERSION:
224 *pdata = synic->version;
226 case HV_X64_MSR_SIEFP:
227 *pdata = synic->evt_page;
229 case HV_X64_MSR_SIMP:
230 *pdata = synic->msg_page;
235 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
236 *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
245 int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
247 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
248 struct kvm_lapic_irq irq;
251 if (sint >= ARRAY_SIZE(synic->sint))
254 vector = synic_get_sint_vector(synic_read_sint(synic, sint));
258 memset(&irq, 0, sizeof(irq));
259 irq.dest_id = kvm_apic_id(vcpu->arch.apic);
260 irq.dest_mode = APIC_DEST_PHYSICAL;
261 irq.delivery_mode = APIC_DM_FIXED;
265 ret = kvm_irq_delivery_to_apic(vcpu->kvm, NULL, &irq, NULL);
266 vcpu_debug(vcpu, "Hyper-V SynIC set irq ret %d\n", ret);
270 int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vcpu_id, u32 sint)
272 struct kvm_vcpu_hv_synic *synic;
274 synic = synic_get(kvm, vcpu_id);
278 return synic_set_irq(synic, sint);
281 void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
283 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
286 vcpu_debug(vcpu, "Hyper-V SynIC send eoi vec %d\n", vector);
288 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
289 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
290 kvm_hv_notify_acked_sint(vcpu, i);
293 static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vcpu_id, u32 sint, int gsi)
295 struct kvm_vcpu_hv_synic *synic;
297 synic = synic_get(kvm, vcpu_id);
301 if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
304 atomic_set(&synic->sint_to_gsi[sint], gsi);
308 void kvm_hv_irq_routing_update(struct kvm *kvm)
310 struct kvm_irq_routing_table *irq_rt;
311 struct kvm_kernel_irq_routing_entry *e;
314 irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
315 lockdep_is_held(&kvm->irq_lock));
317 for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
318 hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
319 if (e->type == KVM_IRQ_ROUTING_HV_SINT)
320 kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
321 e->hv_sint.sint, gsi);
326 static void synic_init(struct kvm_vcpu_hv_synic *synic)
330 memset(synic, 0, sizeof(*synic));
331 synic->version = HV_SYNIC_VERSION_1;
332 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
333 atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
334 atomic_set(&synic->sint_to_gsi[i], -1);
338 void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
340 synic_init(vcpu_to_synic(vcpu));
343 int kvm_hv_activate_synic(struct kvm_vcpu *vcpu)
346 * Hyper-V SynIC auto EOI SINT's are
347 * not compatible with APICV, so deactivate APICV
349 kvm_vcpu_deactivate_apicv(vcpu);
350 vcpu_to_synic(vcpu)->active = true;
354 static bool kvm_hv_msr_partition_wide(u32 msr)
359 case HV_X64_MSR_GUEST_OS_ID:
360 case HV_X64_MSR_HYPERCALL:
361 case HV_X64_MSR_REFERENCE_TSC:
362 case HV_X64_MSR_TIME_REF_COUNT:
363 case HV_X64_MSR_CRASH_CTL:
364 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
365 case HV_X64_MSR_RESET:
373 static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
374 u32 index, u64 *pdata)
376 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
378 if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
381 *pdata = hv->hv_crash_param[index];
385 static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata)
387 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
389 *pdata = hv->hv_crash_ctl;
393 static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host)
395 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
398 hv->hv_crash_ctl = data & HV_X64_MSR_CRASH_CTL_NOTIFY;
400 if (!host && (data & HV_X64_MSR_CRASH_CTL_NOTIFY)) {
402 vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
403 hv->hv_crash_param[0],
404 hv->hv_crash_param[1],
405 hv->hv_crash_param[2],
406 hv->hv_crash_param[3],
407 hv->hv_crash_param[4]);
409 /* Send notification about crash to user space */
410 kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
416 static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
419 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
421 if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
424 hv->hv_crash_param[index] = data;
428 static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
431 struct kvm *kvm = vcpu->kvm;
432 struct kvm_hv *hv = &kvm->arch.hyperv;
435 case HV_X64_MSR_GUEST_OS_ID:
436 hv->hv_guest_os_id = data;
437 /* setting guest os id to zero disables hypercall page */
438 if (!hv->hv_guest_os_id)
439 hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
441 case HV_X64_MSR_HYPERCALL: {
446 /* if guest os id is not set hypercall should remain disabled */
447 if (!hv->hv_guest_os_id)
449 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
450 hv->hv_hypercall = data;
453 gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
454 addr = gfn_to_hva(kvm, gfn);
455 if (kvm_is_error_hva(addr))
457 kvm_x86_ops->patch_hypercall(vcpu, instructions);
458 ((unsigned char *)instructions)[3] = 0xc3; /* ret */
459 if (__copy_to_user((void __user *)addr, instructions, 4))
461 hv->hv_hypercall = data;
462 mark_page_dirty(kvm, gfn);
465 case HV_X64_MSR_REFERENCE_TSC: {
467 HV_REFERENCE_TSC_PAGE tsc_ref;
469 memset(&tsc_ref, 0, sizeof(tsc_ref));
470 hv->hv_tsc_page = data;
471 if (!(data & HV_X64_MSR_TSC_REFERENCE_ENABLE))
473 gfn = data >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
476 gfn << HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT,
477 &tsc_ref, sizeof(tsc_ref)))
479 mark_page_dirty(kvm, gfn);
482 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
483 return kvm_hv_msr_set_crash_data(vcpu,
484 msr - HV_X64_MSR_CRASH_P0,
486 case HV_X64_MSR_CRASH_CTL:
487 return kvm_hv_msr_set_crash_ctl(vcpu, data, host);
488 case HV_X64_MSR_RESET:
490 vcpu_debug(vcpu, "hyper-v reset requested\n");
491 kvm_make_request(KVM_REQ_HV_RESET, vcpu);
495 vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
502 /* Calculate cpu time spent by current task in 100ns units */
503 static u64 current_task_runtime_100ns(void)
505 cputime_t utime, stime;
507 task_cputime_adjusted(current, &utime, &stime);
508 return div_u64(cputime_to_nsecs(utime + stime), 100);
511 static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
513 struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
516 case HV_X64_MSR_APIC_ASSIST_PAGE: {
520 if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
522 if (kvm_lapic_enable_pv_eoi(vcpu, 0))
526 gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT;
527 addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
528 if (kvm_is_error_hva(addr))
530 if (__clear_user((void __user *)addr, PAGE_SIZE))
533 kvm_vcpu_mark_page_dirty(vcpu, gfn);
534 if (kvm_lapic_enable_pv_eoi(vcpu,
535 gfn_to_gpa(gfn) | KVM_MSR_ENABLED))
540 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
542 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
544 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
545 case HV_X64_MSR_VP_RUNTIME:
548 hv->runtime_offset = data - current_task_runtime_100ns();
550 case HV_X64_MSR_SCONTROL:
551 case HV_X64_MSR_SVERSION:
552 case HV_X64_MSR_SIEFP:
553 case HV_X64_MSR_SIMP:
555 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
556 return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host);
558 vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
566 static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
569 struct kvm *kvm = vcpu->kvm;
570 struct kvm_hv *hv = &kvm->arch.hyperv;
573 case HV_X64_MSR_GUEST_OS_ID:
574 data = hv->hv_guest_os_id;
576 case HV_X64_MSR_HYPERCALL:
577 data = hv->hv_hypercall;
579 case HV_X64_MSR_TIME_REF_COUNT: {
581 div_u64(get_kernel_ns() + kvm->arch.kvmclock_offset, 100);
584 case HV_X64_MSR_REFERENCE_TSC:
585 data = hv->hv_tsc_page;
587 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
588 return kvm_hv_msr_get_crash_data(vcpu,
589 msr - HV_X64_MSR_CRASH_P0,
591 case HV_X64_MSR_CRASH_CTL:
592 return kvm_hv_msr_get_crash_ctl(vcpu, pdata);
593 case HV_X64_MSR_RESET:
597 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
605 static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
608 struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
611 case HV_X64_MSR_VP_INDEX: {
615 kvm_for_each_vcpu(r, v, vcpu->kvm) {
624 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
626 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
628 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
629 case HV_X64_MSR_APIC_ASSIST_PAGE:
632 case HV_X64_MSR_VP_RUNTIME:
633 data = current_task_runtime_100ns() + hv->runtime_offset;
635 case HV_X64_MSR_SCONTROL:
636 case HV_X64_MSR_SVERSION:
637 case HV_X64_MSR_SIEFP:
638 case HV_X64_MSR_SIMP:
640 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
641 return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata);
643 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
650 int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
652 if (kvm_hv_msr_partition_wide(msr)) {
655 mutex_lock(&vcpu->kvm->lock);
656 r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
657 mutex_unlock(&vcpu->kvm->lock);
660 return kvm_hv_set_msr(vcpu, msr, data, host);
663 int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
665 if (kvm_hv_msr_partition_wide(msr)) {
668 mutex_lock(&vcpu->kvm->lock);
669 r = kvm_hv_get_msr_pw(vcpu, msr, pdata);
670 mutex_unlock(&vcpu->kvm->lock);
673 return kvm_hv_get_msr(vcpu, msr, pdata);
676 bool kvm_hv_hypercall_enabled(struct kvm *kvm)
678 return kvm->arch.hyperv.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
681 int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
683 u64 param, ingpa, outgpa, ret;
684 uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
688 * hypercall generates UD from non zero cpl and real mode
691 if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
692 kvm_queue_exception(vcpu, UD_VECTOR);
696 longmode = is_64_bit_mode(vcpu);
699 param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
700 (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
701 ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
702 (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
703 outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
704 (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
708 param = kvm_register_read(vcpu, VCPU_REGS_RCX);
709 ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
710 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
714 code = param & 0xffff;
715 fast = (param >> 16) & 0x1;
716 rep_cnt = (param >> 32) & 0xfff;
717 rep_idx = (param >> 48) & 0xfff;
719 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
722 case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
723 kvm_vcpu_on_spin(vcpu);
726 res = HV_STATUS_INVALID_HYPERCALL_CODE;
730 ret = res | (((u64)rep_done & 0xfff) << 32);
732 kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
734 kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
735 kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);