xref: /openbmc/linux/arch/x86/kvm/hyperv.c (revision efc479e6900c22bad9a2b649d13405ed9cde2d53)
1e83d5887SAndrey Smetanin /*
2e83d5887SAndrey Smetanin  * KVM Microsoft Hyper-V emulation
3e83d5887SAndrey Smetanin  *
4e83d5887SAndrey Smetanin  * derived from arch/x86/kvm/x86.c
5e83d5887SAndrey Smetanin  *
6e83d5887SAndrey Smetanin  * Copyright (C) 2006 Qumranet, Inc.
7e83d5887SAndrey Smetanin  * Copyright (C) 2008 Qumranet, Inc.
8e83d5887SAndrey Smetanin  * Copyright IBM Corporation, 2008
9e83d5887SAndrey Smetanin  * Copyright 2010 Red Hat, Inc. and/or its affiliates.
10e83d5887SAndrey Smetanin  * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com>
11e83d5887SAndrey Smetanin  *
12e83d5887SAndrey Smetanin  * Authors:
13e83d5887SAndrey Smetanin  *   Avi Kivity   <avi@qumranet.com>
14e83d5887SAndrey Smetanin  *   Yaniv Kamay  <yaniv@qumranet.com>
15e83d5887SAndrey Smetanin  *   Amit Shah    <amit.shah@qumranet.com>
16e83d5887SAndrey Smetanin  *   Ben-Ami Yassour <benami@il.ibm.com>
17e83d5887SAndrey Smetanin  *   Andrey Smetanin <asmetanin@virtuozzo.com>
18e83d5887SAndrey Smetanin  *
19e83d5887SAndrey Smetanin  * This work is licensed under the terms of the GNU GPL, version 2.  See
20e83d5887SAndrey Smetanin  * the COPYING file in the top-level directory.
21e83d5887SAndrey Smetanin  *
22e83d5887SAndrey Smetanin  */
23e83d5887SAndrey Smetanin 
24e83d5887SAndrey Smetanin #include "x86.h"
25e83d5887SAndrey Smetanin #include "lapic.h"
265c919412SAndrey Smetanin #include "ioapic.h"
27e83d5887SAndrey Smetanin #include "hyperv.h"
28e83d5887SAndrey Smetanin 
29e83d5887SAndrey Smetanin #include <linux/kvm_host.h>
30765eaa0fSAndrey Smetanin #include <linux/highmem.h>
3132ef5517SIngo Molnar #include <linux/sched/cputime.h>
3232ef5517SIngo Molnar 
335c919412SAndrey Smetanin #include <asm/apicdef.h>
34e83d5887SAndrey Smetanin #include <trace/events/kvm.h>
35e83d5887SAndrey Smetanin 
36e83d5887SAndrey Smetanin #include "trace.h"
37e83d5887SAndrey Smetanin 
385c919412SAndrey Smetanin static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
395c919412SAndrey Smetanin {
405c919412SAndrey Smetanin 	return atomic64_read(&synic->sint[sint]);
415c919412SAndrey Smetanin }
425c919412SAndrey Smetanin 
435c919412SAndrey Smetanin static inline int synic_get_sint_vector(u64 sint_value)
445c919412SAndrey Smetanin {
455c919412SAndrey Smetanin 	if (sint_value & HV_SYNIC_SINT_MASKED)
465c919412SAndrey Smetanin 		return -1;
475c919412SAndrey Smetanin 	return sint_value & HV_SYNIC_SINT_VECTOR_MASK;
485c919412SAndrey Smetanin }
495c919412SAndrey Smetanin 
505c919412SAndrey Smetanin static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic,
515c919412SAndrey Smetanin 				      int vector)
525c919412SAndrey Smetanin {
535c919412SAndrey Smetanin 	int i;
545c919412SAndrey Smetanin 
555c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
565c919412SAndrey Smetanin 		if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
575c919412SAndrey Smetanin 			return true;
585c919412SAndrey Smetanin 	}
595c919412SAndrey Smetanin 	return false;
605c919412SAndrey Smetanin }
615c919412SAndrey Smetanin 
625c919412SAndrey Smetanin static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic,
635c919412SAndrey Smetanin 				     int vector)
645c919412SAndrey Smetanin {
655c919412SAndrey Smetanin 	int i;
665c919412SAndrey Smetanin 	u64 sint_value;
675c919412SAndrey Smetanin 
685c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
695c919412SAndrey Smetanin 		sint_value = synic_read_sint(synic, i);
705c919412SAndrey Smetanin 		if (synic_get_sint_vector(sint_value) == vector &&
715c919412SAndrey Smetanin 		    sint_value & HV_SYNIC_SINT_AUTO_EOI)
725c919412SAndrey Smetanin 			return true;
735c919412SAndrey Smetanin 	}
745c919412SAndrey Smetanin 	return false;
755c919412SAndrey Smetanin }
765c919412SAndrey Smetanin 
777be58a64SAndrey Smetanin static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint,
787be58a64SAndrey Smetanin 			  u64 data, bool host)
795c919412SAndrey Smetanin {
805c919412SAndrey Smetanin 	int vector;
815c919412SAndrey Smetanin 
825c919412SAndrey Smetanin 	vector = data & HV_SYNIC_SINT_VECTOR_MASK;
837be58a64SAndrey Smetanin 	if (vector < 16 && !host)
845c919412SAndrey Smetanin 		return 1;
855c919412SAndrey Smetanin 	/*
865c919412SAndrey Smetanin 	 * Guest may configure multiple SINTs to use the same vector, so
875c919412SAndrey Smetanin 	 * we maintain a bitmap of vectors handled by synic, and a
885c919412SAndrey Smetanin 	 * bitmap of vectors with auto-eoi behavior.  The bitmaps are
895c919412SAndrey Smetanin 	 * updated here, and atomically queried on fast paths.
905c919412SAndrey Smetanin 	 */
915c919412SAndrey Smetanin 
925c919412SAndrey Smetanin 	atomic64_set(&synic->sint[sint], data);
935c919412SAndrey Smetanin 
945c919412SAndrey Smetanin 	if (synic_has_vector_connected(synic, vector))
955c919412SAndrey Smetanin 		__set_bit(vector, synic->vec_bitmap);
965c919412SAndrey Smetanin 	else
975c919412SAndrey Smetanin 		__clear_bit(vector, synic->vec_bitmap);
985c919412SAndrey Smetanin 
995c919412SAndrey Smetanin 	if (synic_has_vector_auto_eoi(synic, vector))
1005c919412SAndrey Smetanin 		__set_bit(vector, synic->auto_eoi_bitmap);
1015c919412SAndrey Smetanin 	else
1025c919412SAndrey Smetanin 		__clear_bit(vector, synic->auto_eoi_bitmap);
1035c919412SAndrey Smetanin 
1045c919412SAndrey Smetanin 	/* Load SynIC vectors into EOI exit bitmap */
1055c919412SAndrey Smetanin 	kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic));
1065c919412SAndrey Smetanin 	return 0;
1075c919412SAndrey Smetanin }
1085c919412SAndrey Smetanin 
1095c919412SAndrey Smetanin static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vcpu_id)
1105c919412SAndrey Smetanin {
1115c919412SAndrey Smetanin 	struct kvm_vcpu *vcpu;
1125c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic;
1135c919412SAndrey Smetanin 
1145c919412SAndrey Smetanin 	if (vcpu_id >= atomic_read(&kvm->online_vcpus))
1155c919412SAndrey Smetanin 		return NULL;
1165c919412SAndrey Smetanin 	vcpu = kvm_get_vcpu(kvm, vcpu_id);
1175c919412SAndrey Smetanin 	if (!vcpu)
1185c919412SAndrey Smetanin 		return NULL;
1195c919412SAndrey Smetanin 	synic = vcpu_to_synic(vcpu);
1205c919412SAndrey Smetanin 	return (synic->active) ? synic : NULL;
1215c919412SAndrey Smetanin }
1225c919412SAndrey Smetanin 
123765eaa0fSAndrey Smetanin static void synic_clear_sint_msg_pending(struct kvm_vcpu_hv_synic *synic,
124765eaa0fSAndrey Smetanin 					u32 sint)
125765eaa0fSAndrey Smetanin {
126765eaa0fSAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
127765eaa0fSAndrey Smetanin 	struct page *page;
128765eaa0fSAndrey Smetanin 	gpa_t gpa;
129765eaa0fSAndrey Smetanin 	struct hv_message *msg;
130765eaa0fSAndrey Smetanin 	struct hv_message_page *msg_page;
131765eaa0fSAndrey Smetanin 
132765eaa0fSAndrey Smetanin 	gpa = synic->msg_page & PAGE_MASK;
133765eaa0fSAndrey Smetanin 	page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
134765eaa0fSAndrey Smetanin 	if (is_error_page(page)) {
135765eaa0fSAndrey Smetanin 		vcpu_err(vcpu, "Hyper-V SynIC can't get msg page, gpa 0x%llx\n",
136765eaa0fSAndrey Smetanin 			 gpa);
137765eaa0fSAndrey Smetanin 		return;
138765eaa0fSAndrey Smetanin 	}
139765eaa0fSAndrey Smetanin 	msg_page = kmap_atomic(page);
140765eaa0fSAndrey Smetanin 
141765eaa0fSAndrey Smetanin 	msg = &msg_page->sint_message[sint];
142765eaa0fSAndrey Smetanin 	msg->header.message_flags.msg_pending = 0;
143765eaa0fSAndrey Smetanin 
144765eaa0fSAndrey Smetanin 	kunmap_atomic(msg_page);
145765eaa0fSAndrey Smetanin 	kvm_release_page_dirty(page);
146765eaa0fSAndrey Smetanin 	kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
147765eaa0fSAndrey Smetanin }
148765eaa0fSAndrey Smetanin 
1495c919412SAndrey Smetanin static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
1505c919412SAndrey Smetanin {
1515c919412SAndrey Smetanin 	struct kvm *kvm = vcpu->kvm;
152765eaa0fSAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
1531f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
1541f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv_stimer *stimer;
1551f4b34f8SAndrey Smetanin 	int gsi, idx, stimers_pending;
1565c919412SAndrey Smetanin 
15718659a9cSAndrey Smetanin 	trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint);
1585c919412SAndrey Smetanin 
159765eaa0fSAndrey Smetanin 	if (synic->msg_page & HV_SYNIC_SIMP_ENABLE)
160765eaa0fSAndrey Smetanin 		synic_clear_sint_msg_pending(synic, sint);
161765eaa0fSAndrey Smetanin 
1621f4b34f8SAndrey Smetanin 	/* Try to deliver pending Hyper-V SynIC timers messages */
1631f4b34f8SAndrey Smetanin 	stimers_pending = 0;
1641f4b34f8SAndrey Smetanin 	for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) {
1651f4b34f8SAndrey Smetanin 		stimer = &hv_vcpu->stimer[idx];
1661f4b34f8SAndrey Smetanin 		if (stimer->msg_pending &&
1671f4b34f8SAndrey Smetanin 		    (stimer->config & HV_STIMER_ENABLE) &&
1681f4b34f8SAndrey Smetanin 		    HV_STIMER_SINT(stimer->config) == sint) {
1691f4b34f8SAndrey Smetanin 			set_bit(stimer->index,
1701f4b34f8SAndrey Smetanin 				hv_vcpu->stimer_pending_bitmap);
1711f4b34f8SAndrey Smetanin 			stimers_pending++;
1721f4b34f8SAndrey Smetanin 		}
1731f4b34f8SAndrey Smetanin 	}
1741f4b34f8SAndrey Smetanin 	if (stimers_pending)
1751f4b34f8SAndrey Smetanin 		kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
1761f4b34f8SAndrey Smetanin 
1775c919412SAndrey Smetanin 	idx = srcu_read_lock(&kvm->irq_srcu);
1781f4b34f8SAndrey Smetanin 	gsi = atomic_read(&synic->sint_to_gsi[sint]);
1795c919412SAndrey Smetanin 	if (gsi != -1)
1805c919412SAndrey Smetanin 		kvm_notify_acked_gsi(kvm, gsi);
1815c919412SAndrey Smetanin 	srcu_read_unlock(&kvm->irq_srcu, idx);
1825c919412SAndrey Smetanin }
1835c919412SAndrey Smetanin 
184db397571SAndrey Smetanin static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
185db397571SAndrey Smetanin {
186db397571SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
187db397571SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
188db397571SAndrey Smetanin 
189db397571SAndrey Smetanin 	hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
190db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.msr = msr;
191db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.control = synic->control;
192db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
193db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
194db397571SAndrey Smetanin 
195db397571SAndrey Smetanin 	kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
196db397571SAndrey Smetanin }
197db397571SAndrey Smetanin 
1985c919412SAndrey Smetanin static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
1995c919412SAndrey Smetanin 			 u32 msr, u64 data, bool host)
2005c919412SAndrey Smetanin {
2015c919412SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
2025c919412SAndrey Smetanin 	int ret;
2035c919412SAndrey Smetanin 
2045c919412SAndrey Smetanin 	if (!synic->active)
2055c919412SAndrey Smetanin 		return 1;
2065c919412SAndrey Smetanin 
20718659a9cSAndrey Smetanin 	trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host);
20818659a9cSAndrey Smetanin 
2095c919412SAndrey Smetanin 	ret = 0;
2105c919412SAndrey Smetanin 	switch (msr) {
2115c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
2125c919412SAndrey Smetanin 		synic->control = data;
213db397571SAndrey Smetanin 		if (!host)
214db397571SAndrey Smetanin 			synic_exit(synic, msr);
2155c919412SAndrey Smetanin 		break;
2165c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
2175c919412SAndrey Smetanin 		if (!host) {
2185c919412SAndrey Smetanin 			ret = 1;
2195c919412SAndrey Smetanin 			break;
2205c919412SAndrey Smetanin 		}
2215c919412SAndrey Smetanin 		synic->version = data;
2225c919412SAndrey Smetanin 		break;
2235c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
224*efc479e6SRoman Kagan 		if ((data & HV_SYNIC_SIEFP_ENABLE) && !host &&
225*efc479e6SRoman Kagan 		    !synic->dont_zero_synic_pages)
2265c919412SAndrey Smetanin 			if (kvm_clear_guest(vcpu->kvm,
2275c919412SAndrey Smetanin 					    data & PAGE_MASK, PAGE_SIZE)) {
2285c919412SAndrey Smetanin 				ret = 1;
2295c919412SAndrey Smetanin 				break;
2305c919412SAndrey Smetanin 			}
2315c919412SAndrey Smetanin 		synic->evt_page = data;
232db397571SAndrey Smetanin 		if (!host)
233db397571SAndrey Smetanin 			synic_exit(synic, msr);
2345c919412SAndrey Smetanin 		break;
2355c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
236*efc479e6SRoman Kagan 		if ((data & HV_SYNIC_SIMP_ENABLE) && !host &&
237*efc479e6SRoman Kagan 		    !synic->dont_zero_synic_pages)
2385c919412SAndrey Smetanin 			if (kvm_clear_guest(vcpu->kvm,
2395c919412SAndrey Smetanin 					    data & PAGE_MASK, PAGE_SIZE)) {
2405c919412SAndrey Smetanin 				ret = 1;
2415c919412SAndrey Smetanin 				break;
2425c919412SAndrey Smetanin 			}
2435c919412SAndrey Smetanin 		synic->msg_page = data;
244db397571SAndrey Smetanin 		if (!host)
245db397571SAndrey Smetanin 			synic_exit(synic, msr);
2465c919412SAndrey Smetanin 		break;
2475c919412SAndrey Smetanin 	case HV_X64_MSR_EOM: {
2485c919412SAndrey Smetanin 		int i;
2495c919412SAndrey Smetanin 
2505c919412SAndrey Smetanin 		for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
2515c919412SAndrey Smetanin 			kvm_hv_notify_acked_sint(vcpu, i);
2525c919412SAndrey Smetanin 		break;
2535c919412SAndrey Smetanin 	}
2545c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
2557be58a64SAndrey Smetanin 		ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host);
2565c919412SAndrey Smetanin 		break;
2575c919412SAndrey Smetanin 	default:
2585c919412SAndrey Smetanin 		ret = 1;
2595c919412SAndrey Smetanin 		break;
2605c919412SAndrey Smetanin 	}
2615c919412SAndrey Smetanin 	return ret;
2625c919412SAndrey Smetanin }
2635c919412SAndrey Smetanin 
2645c919412SAndrey Smetanin static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata)
2655c919412SAndrey Smetanin {
2665c919412SAndrey Smetanin 	int ret;
2675c919412SAndrey Smetanin 
2685c919412SAndrey Smetanin 	if (!synic->active)
2695c919412SAndrey Smetanin 		return 1;
2705c919412SAndrey Smetanin 
2715c919412SAndrey Smetanin 	ret = 0;
2725c919412SAndrey Smetanin 	switch (msr) {
2735c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
2745c919412SAndrey Smetanin 		*pdata = synic->control;
2755c919412SAndrey Smetanin 		break;
2765c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
2775c919412SAndrey Smetanin 		*pdata = synic->version;
2785c919412SAndrey Smetanin 		break;
2795c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
2805c919412SAndrey Smetanin 		*pdata = synic->evt_page;
2815c919412SAndrey Smetanin 		break;
2825c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
2835c919412SAndrey Smetanin 		*pdata = synic->msg_page;
2845c919412SAndrey Smetanin 		break;
2855c919412SAndrey Smetanin 	case HV_X64_MSR_EOM:
2865c919412SAndrey Smetanin 		*pdata = 0;
2875c919412SAndrey Smetanin 		break;
2885c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
2895c919412SAndrey Smetanin 		*pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
2905c919412SAndrey Smetanin 		break;
2915c919412SAndrey Smetanin 	default:
2925c919412SAndrey Smetanin 		ret = 1;
2935c919412SAndrey Smetanin 		break;
2945c919412SAndrey Smetanin 	}
2955c919412SAndrey Smetanin 	return ret;
2965c919412SAndrey Smetanin }
2975c919412SAndrey Smetanin 
298ecd8a8c2SJiang Biao static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
2995c919412SAndrey Smetanin {
3005c919412SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
3015c919412SAndrey Smetanin 	struct kvm_lapic_irq irq;
3025c919412SAndrey Smetanin 	int ret, vector;
3035c919412SAndrey Smetanin 
3045c919412SAndrey Smetanin 	if (sint >= ARRAY_SIZE(synic->sint))
3055c919412SAndrey Smetanin 		return -EINVAL;
3065c919412SAndrey Smetanin 
3075c919412SAndrey Smetanin 	vector = synic_get_sint_vector(synic_read_sint(synic, sint));
3085c919412SAndrey Smetanin 	if (vector < 0)
3095c919412SAndrey Smetanin 		return -ENOENT;
3105c919412SAndrey Smetanin 
3115c919412SAndrey Smetanin 	memset(&irq, 0, sizeof(irq));
312f98a3efbSRadim Krčmář 	irq.shorthand = APIC_DEST_SELF;
3135c919412SAndrey Smetanin 	irq.dest_mode = APIC_DEST_PHYSICAL;
3145c919412SAndrey Smetanin 	irq.delivery_mode = APIC_DM_FIXED;
3155c919412SAndrey Smetanin 	irq.vector = vector;
3165c919412SAndrey Smetanin 	irq.level = 1;
3175c919412SAndrey Smetanin 
318f98a3efbSRadim Krčmář 	ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq, NULL);
31918659a9cSAndrey Smetanin 	trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret);
3205c919412SAndrey Smetanin 	return ret;
3215c919412SAndrey Smetanin }
3225c919412SAndrey Smetanin 
3235c919412SAndrey Smetanin int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vcpu_id, u32 sint)
3245c919412SAndrey Smetanin {
3255c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic;
3265c919412SAndrey Smetanin 
3275c919412SAndrey Smetanin 	synic = synic_get(kvm, vcpu_id);
3285c919412SAndrey Smetanin 	if (!synic)
3295c919412SAndrey Smetanin 		return -EINVAL;
3305c919412SAndrey Smetanin 
3315c919412SAndrey Smetanin 	return synic_set_irq(synic, sint);
3325c919412SAndrey Smetanin }
3335c919412SAndrey Smetanin 
3345c919412SAndrey Smetanin void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
3355c919412SAndrey Smetanin {
3365c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
3375c919412SAndrey Smetanin 	int i;
3385c919412SAndrey Smetanin 
33918659a9cSAndrey Smetanin 	trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector);
3405c919412SAndrey Smetanin 
3415c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
3425c919412SAndrey Smetanin 		if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
3435c919412SAndrey Smetanin 			kvm_hv_notify_acked_sint(vcpu, i);
3445c919412SAndrey Smetanin }
3455c919412SAndrey Smetanin 
3465c919412SAndrey Smetanin static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vcpu_id, u32 sint, int gsi)
3475c919412SAndrey Smetanin {
3485c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic;
3495c919412SAndrey Smetanin 
3505c919412SAndrey Smetanin 	synic = synic_get(kvm, vcpu_id);
3515c919412SAndrey Smetanin 	if (!synic)
3525c919412SAndrey Smetanin 		return -EINVAL;
3535c919412SAndrey Smetanin 
3545c919412SAndrey Smetanin 	if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
3555c919412SAndrey Smetanin 		return -EINVAL;
3565c919412SAndrey Smetanin 
3575c919412SAndrey Smetanin 	atomic_set(&synic->sint_to_gsi[sint], gsi);
3585c919412SAndrey Smetanin 	return 0;
3595c919412SAndrey Smetanin }
3605c919412SAndrey Smetanin 
3615c919412SAndrey Smetanin void kvm_hv_irq_routing_update(struct kvm *kvm)
3625c919412SAndrey Smetanin {
3635c919412SAndrey Smetanin 	struct kvm_irq_routing_table *irq_rt;
3645c919412SAndrey Smetanin 	struct kvm_kernel_irq_routing_entry *e;
3655c919412SAndrey Smetanin 	u32 gsi;
3665c919412SAndrey Smetanin 
3675c919412SAndrey Smetanin 	irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
3685c919412SAndrey Smetanin 					lockdep_is_held(&kvm->irq_lock));
3695c919412SAndrey Smetanin 
3705c919412SAndrey Smetanin 	for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
3715c919412SAndrey Smetanin 		hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
3725c919412SAndrey Smetanin 			if (e->type == KVM_IRQ_ROUTING_HV_SINT)
3735c919412SAndrey Smetanin 				kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
3745c919412SAndrey Smetanin 						    e->hv_sint.sint, gsi);
3755c919412SAndrey Smetanin 		}
3765c919412SAndrey Smetanin 	}
3775c919412SAndrey Smetanin }
3785c919412SAndrey Smetanin 
3795c919412SAndrey Smetanin static void synic_init(struct kvm_vcpu_hv_synic *synic)
3805c919412SAndrey Smetanin {
3815c919412SAndrey Smetanin 	int i;
3825c919412SAndrey Smetanin 
3835c919412SAndrey Smetanin 	memset(synic, 0, sizeof(*synic));
3845c919412SAndrey Smetanin 	synic->version = HV_SYNIC_VERSION_1;
3855c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
3865c919412SAndrey Smetanin 		atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
3875c919412SAndrey Smetanin 		atomic_set(&synic->sint_to_gsi[i], -1);
3885c919412SAndrey Smetanin 	}
3895c919412SAndrey Smetanin }
3905c919412SAndrey Smetanin 
39193bf4172SAndrey Smetanin static u64 get_time_ref_counter(struct kvm *kvm)
39293bf4172SAndrey Smetanin {
393095cf55dSPaolo Bonzini 	struct kvm_hv *hv = &kvm->arch.hyperv;
394095cf55dSPaolo Bonzini 	struct kvm_vcpu *vcpu;
395095cf55dSPaolo Bonzini 	u64 tsc;
396095cf55dSPaolo Bonzini 
397095cf55dSPaolo Bonzini 	/*
398095cf55dSPaolo Bonzini 	 * The guest has not set up the TSC page or the clock isn't
399095cf55dSPaolo Bonzini 	 * stable, fall back to get_kvmclock_ns.
400095cf55dSPaolo Bonzini 	 */
401095cf55dSPaolo Bonzini 	if (!hv->tsc_ref.tsc_sequence)
402108b249cSPaolo Bonzini 		return div_u64(get_kvmclock_ns(kvm), 100);
403095cf55dSPaolo Bonzini 
404095cf55dSPaolo Bonzini 	vcpu = kvm_get_vcpu(kvm, 0);
405095cf55dSPaolo Bonzini 	tsc = kvm_read_l1_tsc(vcpu, rdtsc());
406095cf55dSPaolo Bonzini 	return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64)
407095cf55dSPaolo Bonzini 		+ hv->tsc_ref.tsc_offset;
40893bf4172SAndrey Smetanin }
40993bf4172SAndrey Smetanin 
410f3b138c5SAndrey Smetanin static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
4111f4b34f8SAndrey Smetanin 				bool vcpu_kick)
4121f4b34f8SAndrey Smetanin {
4131f4b34f8SAndrey Smetanin 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
4141f4b34f8SAndrey Smetanin 
4151f4b34f8SAndrey Smetanin 	set_bit(stimer->index,
4161f4b34f8SAndrey Smetanin 		vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
4171f4b34f8SAndrey Smetanin 	kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
4181f4b34f8SAndrey Smetanin 	if (vcpu_kick)
4191f4b34f8SAndrey Smetanin 		kvm_vcpu_kick(vcpu);
4201f4b34f8SAndrey Smetanin }
4211f4b34f8SAndrey Smetanin 
4221f4b34f8SAndrey Smetanin static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
4231f4b34f8SAndrey Smetanin {
4241f4b34f8SAndrey Smetanin 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
4251f4b34f8SAndrey Smetanin 
426ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_cleanup(stimer_to_vcpu(stimer)->vcpu_id,
427ac3e5fcaSAndrey Smetanin 				    stimer->index);
428ac3e5fcaSAndrey Smetanin 
429019b9781SAndrey Smetanin 	hrtimer_cancel(&stimer->timer);
4301f4b34f8SAndrey Smetanin 	clear_bit(stimer->index,
4311f4b34f8SAndrey Smetanin 		  vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
4321f4b34f8SAndrey Smetanin 	stimer->msg_pending = false;
433f808495dSAndrey Smetanin 	stimer->exp_time = 0;
4341f4b34f8SAndrey Smetanin }
4351f4b34f8SAndrey Smetanin 
4361f4b34f8SAndrey Smetanin static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
4371f4b34f8SAndrey Smetanin {
4381f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv_stimer *stimer;
4391f4b34f8SAndrey Smetanin 
4401f4b34f8SAndrey Smetanin 	stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
441ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_callback(stimer_to_vcpu(stimer)->vcpu_id,
442ac3e5fcaSAndrey Smetanin 				     stimer->index);
443f3b138c5SAndrey Smetanin 	stimer_mark_pending(stimer, true);
4441f4b34f8SAndrey Smetanin 
4451f4b34f8SAndrey Smetanin 	return HRTIMER_NORESTART;
4461f4b34f8SAndrey Smetanin }
4471f4b34f8SAndrey Smetanin 
448f808495dSAndrey Smetanin /*
449f808495dSAndrey Smetanin  * stimer_start() assumptions:
450f808495dSAndrey Smetanin  * a) stimer->count is not equal to 0
451f808495dSAndrey Smetanin  * b) stimer->config has HV_STIMER_ENABLE flag
452f808495dSAndrey Smetanin  */
4531f4b34f8SAndrey Smetanin static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
4541f4b34f8SAndrey Smetanin {
4551f4b34f8SAndrey Smetanin 	u64 time_now;
4561f4b34f8SAndrey Smetanin 	ktime_t ktime_now;
4571f4b34f8SAndrey Smetanin 
4581f4b34f8SAndrey Smetanin 	time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm);
4591f4b34f8SAndrey Smetanin 	ktime_now = ktime_get();
4601f4b34f8SAndrey Smetanin 
4611f4b34f8SAndrey Smetanin 	if (stimer->config & HV_STIMER_PERIODIC) {
462f808495dSAndrey Smetanin 		if (stimer->exp_time) {
463f808495dSAndrey Smetanin 			if (time_now >= stimer->exp_time) {
464f808495dSAndrey Smetanin 				u64 remainder;
4651f4b34f8SAndrey Smetanin 
466f808495dSAndrey Smetanin 				div64_u64_rem(time_now - stimer->exp_time,
467f808495dSAndrey Smetanin 					      stimer->count, &remainder);
468f808495dSAndrey Smetanin 				stimer->exp_time =
469f808495dSAndrey Smetanin 					time_now + (stimer->count - remainder);
470f808495dSAndrey Smetanin 			}
471f808495dSAndrey Smetanin 		} else
4721f4b34f8SAndrey Smetanin 			stimer->exp_time = time_now + stimer->count;
473f808495dSAndrey Smetanin 
474ac3e5fcaSAndrey Smetanin 		trace_kvm_hv_stimer_start_periodic(
475ac3e5fcaSAndrey Smetanin 					stimer_to_vcpu(stimer)->vcpu_id,
476ac3e5fcaSAndrey Smetanin 					stimer->index,
477ac3e5fcaSAndrey Smetanin 					time_now, stimer->exp_time);
478ac3e5fcaSAndrey Smetanin 
4791f4b34f8SAndrey Smetanin 		hrtimer_start(&stimer->timer,
480f808495dSAndrey Smetanin 			      ktime_add_ns(ktime_now,
481f808495dSAndrey Smetanin 					   100 * (stimer->exp_time - time_now)),
4821f4b34f8SAndrey Smetanin 			      HRTIMER_MODE_ABS);
4831f4b34f8SAndrey Smetanin 		return 0;
4841f4b34f8SAndrey Smetanin 	}
4851f4b34f8SAndrey Smetanin 	stimer->exp_time = stimer->count;
4861f4b34f8SAndrey Smetanin 	if (time_now >= stimer->count) {
4871f4b34f8SAndrey Smetanin 		/*
4881f4b34f8SAndrey Smetanin 		 * Expire timer according to Hypervisor Top-Level Functional
4891f4b34f8SAndrey Smetanin 		 * specification v4(15.3.1):
4901f4b34f8SAndrey Smetanin 		 * "If a one shot is enabled and the specified count is in
4911f4b34f8SAndrey Smetanin 		 * the past, it will expire immediately."
4921f4b34f8SAndrey Smetanin 		 */
493f3b138c5SAndrey Smetanin 		stimer_mark_pending(stimer, false);
4941f4b34f8SAndrey Smetanin 		return 0;
4951f4b34f8SAndrey Smetanin 	}
4961f4b34f8SAndrey Smetanin 
497ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_start_one_shot(stimer_to_vcpu(stimer)->vcpu_id,
498ac3e5fcaSAndrey Smetanin 					   stimer->index,
499ac3e5fcaSAndrey Smetanin 					   time_now, stimer->count);
500ac3e5fcaSAndrey Smetanin 
5011f4b34f8SAndrey Smetanin 	hrtimer_start(&stimer->timer,
5021f4b34f8SAndrey Smetanin 		      ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
5031f4b34f8SAndrey Smetanin 		      HRTIMER_MODE_ABS);
5041f4b34f8SAndrey Smetanin 	return 0;
5051f4b34f8SAndrey Smetanin }
5061f4b34f8SAndrey Smetanin 
5071f4b34f8SAndrey Smetanin static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
5081f4b34f8SAndrey Smetanin 			     bool host)
5091f4b34f8SAndrey Smetanin {
510ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_set_config(stimer_to_vcpu(stimer)->vcpu_id,
511ac3e5fcaSAndrey Smetanin 				       stimer->index, config, host);
512ac3e5fcaSAndrey Smetanin 
513f3b138c5SAndrey Smetanin 	stimer_cleanup(stimer);
51423a3b201SAndrey Smetanin 	if ((stimer->config & HV_STIMER_ENABLE) && HV_STIMER_SINT(config) == 0)
5151f4b34f8SAndrey Smetanin 		config &= ~HV_STIMER_ENABLE;
5161f4b34f8SAndrey Smetanin 	stimer->config = config;
517f3b138c5SAndrey Smetanin 	stimer_mark_pending(stimer, false);
5181f4b34f8SAndrey Smetanin 	return 0;
5191f4b34f8SAndrey Smetanin }
5201f4b34f8SAndrey Smetanin 
5211f4b34f8SAndrey Smetanin static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
5221f4b34f8SAndrey Smetanin 			    bool host)
5231f4b34f8SAndrey Smetanin {
524ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_set_count(stimer_to_vcpu(stimer)->vcpu_id,
525ac3e5fcaSAndrey Smetanin 				      stimer->index, count, host);
526ac3e5fcaSAndrey Smetanin 
5271f4b34f8SAndrey Smetanin 	stimer_cleanup(stimer);
528f3b138c5SAndrey Smetanin 	stimer->count = count;
5291f4b34f8SAndrey Smetanin 	if (stimer->count == 0)
5301f4b34f8SAndrey Smetanin 		stimer->config &= ~HV_STIMER_ENABLE;
531f3b138c5SAndrey Smetanin 	else if (stimer->config & HV_STIMER_AUTOENABLE)
5321f4b34f8SAndrey Smetanin 		stimer->config |= HV_STIMER_ENABLE;
533f3b138c5SAndrey Smetanin 	stimer_mark_pending(stimer, false);
5341f4b34f8SAndrey Smetanin 	return 0;
5351f4b34f8SAndrey Smetanin }
5361f4b34f8SAndrey Smetanin 
5371f4b34f8SAndrey Smetanin static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
5381f4b34f8SAndrey Smetanin {
5391f4b34f8SAndrey Smetanin 	*pconfig = stimer->config;
5401f4b34f8SAndrey Smetanin 	return 0;
5411f4b34f8SAndrey Smetanin }
5421f4b34f8SAndrey Smetanin 
5431f4b34f8SAndrey Smetanin static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
5441f4b34f8SAndrey Smetanin {
5451f4b34f8SAndrey Smetanin 	*pcount = stimer->count;
5461f4b34f8SAndrey Smetanin 	return 0;
5471f4b34f8SAndrey Smetanin }
5481f4b34f8SAndrey Smetanin 
5491f4b34f8SAndrey Smetanin static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
5501f4b34f8SAndrey Smetanin 			     struct hv_message *src_msg)
5511f4b34f8SAndrey Smetanin {
5521f4b34f8SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
5531f4b34f8SAndrey Smetanin 	struct page *page;
5541f4b34f8SAndrey Smetanin 	gpa_t gpa;
5551f4b34f8SAndrey Smetanin 	struct hv_message *dst_msg;
5561f4b34f8SAndrey Smetanin 	int r;
5571f4b34f8SAndrey Smetanin 	struct hv_message_page *msg_page;
5581f4b34f8SAndrey Smetanin 
5591f4b34f8SAndrey Smetanin 	if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
5601f4b34f8SAndrey Smetanin 		return -ENOENT;
5611f4b34f8SAndrey Smetanin 
5621f4b34f8SAndrey Smetanin 	gpa = synic->msg_page & PAGE_MASK;
5631f4b34f8SAndrey Smetanin 	page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
5641f4b34f8SAndrey Smetanin 	if (is_error_page(page))
5651f4b34f8SAndrey Smetanin 		return -EFAULT;
5661f4b34f8SAndrey Smetanin 
5671f4b34f8SAndrey Smetanin 	msg_page = kmap_atomic(page);
5681f4b34f8SAndrey Smetanin 	dst_msg = &msg_page->sint_message[sint];
5691f4b34f8SAndrey Smetanin 	if (sync_cmpxchg(&dst_msg->header.message_type, HVMSG_NONE,
5701f4b34f8SAndrey Smetanin 			 src_msg->header.message_type) != HVMSG_NONE) {
5711f4b34f8SAndrey Smetanin 		dst_msg->header.message_flags.msg_pending = 1;
5721f4b34f8SAndrey Smetanin 		r = -EAGAIN;
5731f4b34f8SAndrey Smetanin 	} else {
5741f4b34f8SAndrey Smetanin 		memcpy(&dst_msg->u.payload, &src_msg->u.payload,
5751f4b34f8SAndrey Smetanin 		       src_msg->header.payload_size);
5761f4b34f8SAndrey Smetanin 		dst_msg->header.message_type = src_msg->header.message_type;
5771f4b34f8SAndrey Smetanin 		dst_msg->header.payload_size = src_msg->header.payload_size;
5781f4b34f8SAndrey Smetanin 		r = synic_set_irq(synic, sint);
5791f4b34f8SAndrey Smetanin 		if (r >= 1)
5801f4b34f8SAndrey Smetanin 			r = 0;
5811f4b34f8SAndrey Smetanin 		else if (r == 0)
5821f4b34f8SAndrey Smetanin 			r = -EFAULT;
5831f4b34f8SAndrey Smetanin 	}
5841f4b34f8SAndrey Smetanin 	kunmap_atomic(msg_page);
5851f4b34f8SAndrey Smetanin 	kvm_release_page_dirty(page);
5861f4b34f8SAndrey Smetanin 	kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
5871f4b34f8SAndrey Smetanin 	return r;
5881f4b34f8SAndrey Smetanin }
5891f4b34f8SAndrey Smetanin 
5900cdeabb1SAndrey Smetanin static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
5911f4b34f8SAndrey Smetanin {
5921f4b34f8SAndrey Smetanin 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
5931f4b34f8SAndrey Smetanin 	struct hv_message *msg = &stimer->msg;
5941f4b34f8SAndrey Smetanin 	struct hv_timer_message_payload *payload =
5951f4b34f8SAndrey Smetanin 			(struct hv_timer_message_payload *)&msg->u.payload;
5961f4b34f8SAndrey Smetanin 
5971f4b34f8SAndrey Smetanin 	payload->expiration_time = stimer->exp_time;
5981f4b34f8SAndrey Smetanin 	payload->delivery_time = get_time_ref_counter(vcpu->kvm);
5990cdeabb1SAndrey Smetanin 	return synic_deliver_msg(vcpu_to_synic(vcpu),
6001f4b34f8SAndrey Smetanin 				 HV_STIMER_SINT(stimer->config), msg);
6011f4b34f8SAndrey Smetanin }
6021f4b34f8SAndrey Smetanin 
6031f4b34f8SAndrey Smetanin static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
6041f4b34f8SAndrey Smetanin {
605ac3e5fcaSAndrey Smetanin 	int r;
606ac3e5fcaSAndrey Smetanin 
6070cdeabb1SAndrey Smetanin 	stimer->msg_pending = true;
608ac3e5fcaSAndrey Smetanin 	r = stimer_send_msg(stimer);
609ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_expiration(stimer_to_vcpu(stimer)->vcpu_id,
610ac3e5fcaSAndrey Smetanin 				       stimer->index, r);
611ac3e5fcaSAndrey Smetanin 	if (!r) {
6120cdeabb1SAndrey Smetanin 		stimer->msg_pending = false;
6131f4b34f8SAndrey Smetanin 		if (!(stimer->config & HV_STIMER_PERIODIC))
6141ac1b65aSAndrey Smetanin 			stimer->config &= ~HV_STIMER_ENABLE;
6150cdeabb1SAndrey Smetanin 	}
6161f4b34f8SAndrey Smetanin }
6171f4b34f8SAndrey Smetanin 
6181f4b34f8SAndrey Smetanin void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
6191f4b34f8SAndrey Smetanin {
6201f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
6211f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv_stimer *stimer;
622f3b138c5SAndrey Smetanin 	u64 time_now, exp_time;
6231f4b34f8SAndrey Smetanin 	int i;
6241f4b34f8SAndrey Smetanin 
6251f4b34f8SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
6261f4b34f8SAndrey Smetanin 		if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
6271f4b34f8SAndrey Smetanin 			stimer = &hv_vcpu->stimer[i];
6281f4b34f8SAndrey Smetanin 			if (stimer->config & HV_STIMER_ENABLE) {
629f3b138c5SAndrey Smetanin 				exp_time = stimer->exp_time;
6300cdeabb1SAndrey Smetanin 
631f3b138c5SAndrey Smetanin 				if (exp_time) {
632f3b138c5SAndrey Smetanin 					time_now =
633f3b138c5SAndrey Smetanin 						get_time_ref_counter(vcpu->kvm);
634f3b138c5SAndrey Smetanin 					if (time_now >= exp_time)
635f3b138c5SAndrey Smetanin 						stimer_expiration(stimer);
636f3b138c5SAndrey Smetanin 				}
637f3b138c5SAndrey Smetanin 
638f3b138c5SAndrey Smetanin 				if ((stimer->config & HV_STIMER_ENABLE) &&
639f3b138c5SAndrey Smetanin 				    stimer->count)
6400cdeabb1SAndrey Smetanin 					stimer_start(stimer);
6410cdeabb1SAndrey Smetanin 				else
6420cdeabb1SAndrey Smetanin 					stimer_cleanup(stimer);
6431f4b34f8SAndrey Smetanin 			}
6441f4b34f8SAndrey Smetanin 		}
6451f4b34f8SAndrey Smetanin }
6461f4b34f8SAndrey Smetanin 
6471f4b34f8SAndrey Smetanin void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)
6481f4b34f8SAndrey Smetanin {
6491f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
6501f4b34f8SAndrey Smetanin 	int i;
6511f4b34f8SAndrey Smetanin 
6521f4b34f8SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
6531f4b34f8SAndrey Smetanin 		stimer_cleanup(&hv_vcpu->stimer[i]);
6541f4b34f8SAndrey Smetanin }
6551f4b34f8SAndrey Smetanin 
6561f4b34f8SAndrey Smetanin static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer)
6571f4b34f8SAndrey Smetanin {
6581f4b34f8SAndrey Smetanin 	struct hv_message *msg = &stimer->msg;
6591f4b34f8SAndrey Smetanin 	struct hv_timer_message_payload *payload =
6601f4b34f8SAndrey Smetanin 			(struct hv_timer_message_payload *)&msg->u.payload;
6611f4b34f8SAndrey Smetanin 
6621f4b34f8SAndrey Smetanin 	memset(&msg->header, 0, sizeof(msg->header));
6631f4b34f8SAndrey Smetanin 	msg->header.message_type = HVMSG_TIMER_EXPIRED;
6641f4b34f8SAndrey Smetanin 	msg->header.payload_size = sizeof(*payload);
6651f4b34f8SAndrey Smetanin 
6661f4b34f8SAndrey Smetanin 	payload->timer_index = stimer->index;
6671f4b34f8SAndrey Smetanin 	payload->expiration_time = 0;
6681f4b34f8SAndrey Smetanin 	payload->delivery_time = 0;
6691f4b34f8SAndrey Smetanin }
6701f4b34f8SAndrey Smetanin 
6711f4b34f8SAndrey Smetanin static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index)
6721f4b34f8SAndrey Smetanin {
6731f4b34f8SAndrey Smetanin 	memset(stimer, 0, sizeof(*stimer));
6741f4b34f8SAndrey Smetanin 	stimer->index = timer_index;
6751f4b34f8SAndrey Smetanin 	hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
6761f4b34f8SAndrey Smetanin 	stimer->timer.function = stimer_timer_callback;
6771f4b34f8SAndrey Smetanin 	stimer_prepare_msg(stimer);
6781f4b34f8SAndrey Smetanin }
6791f4b34f8SAndrey Smetanin 
6805c919412SAndrey Smetanin void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
6815c919412SAndrey Smetanin {
6821f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
6831f4b34f8SAndrey Smetanin 	int i;
6841f4b34f8SAndrey Smetanin 
6851f4b34f8SAndrey Smetanin 	synic_init(&hv_vcpu->synic);
6861f4b34f8SAndrey Smetanin 
6871f4b34f8SAndrey Smetanin 	bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
6881f4b34f8SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
6891f4b34f8SAndrey Smetanin 		stimer_init(&hv_vcpu->stimer[i], i);
6905c919412SAndrey Smetanin }
6915c919412SAndrey Smetanin 
692*efc479e6SRoman Kagan int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages)
6935c919412SAndrey Smetanin {
694*efc479e6SRoman Kagan 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
695*efc479e6SRoman Kagan 
6965c919412SAndrey Smetanin 	/*
6975c919412SAndrey Smetanin 	 * Hyper-V SynIC auto EOI SINT's are
6985c919412SAndrey Smetanin 	 * not compatible with APICV, so deactivate APICV
6995c919412SAndrey Smetanin 	 */
7005c919412SAndrey Smetanin 	kvm_vcpu_deactivate_apicv(vcpu);
701*efc479e6SRoman Kagan 	synic->active = true;
702*efc479e6SRoman Kagan 	synic->dont_zero_synic_pages = dont_zero_synic_pages;
7035c919412SAndrey Smetanin 	return 0;
7045c919412SAndrey Smetanin }
7055c919412SAndrey Smetanin 
706e83d5887SAndrey Smetanin static bool kvm_hv_msr_partition_wide(u32 msr)
707e83d5887SAndrey Smetanin {
708e83d5887SAndrey Smetanin 	bool r = false;
709e83d5887SAndrey Smetanin 
710e83d5887SAndrey Smetanin 	switch (msr) {
711e83d5887SAndrey Smetanin 	case HV_X64_MSR_GUEST_OS_ID:
712e83d5887SAndrey Smetanin 	case HV_X64_MSR_HYPERCALL:
713e83d5887SAndrey Smetanin 	case HV_X64_MSR_REFERENCE_TSC:
714e83d5887SAndrey Smetanin 	case HV_X64_MSR_TIME_REF_COUNT:
715e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_CTL:
716e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
717e516cebbSAndrey Smetanin 	case HV_X64_MSR_RESET:
718e83d5887SAndrey Smetanin 		r = true;
719e83d5887SAndrey Smetanin 		break;
720e83d5887SAndrey Smetanin 	}
721e83d5887SAndrey Smetanin 
722e83d5887SAndrey Smetanin 	return r;
723e83d5887SAndrey Smetanin }
724e83d5887SAndrey Smetanin 
725e7d9513bSAndrey Smetanin static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
726e7d9513bSAndrey Smetanin 				     u32 index, u64 *pdata)
727e7d9513bSAndrey Smetanin {
728e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
729e7d9513bSAndrey Smetanin 
730e7d9513bSAndrey Smetanin 	if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
731e7d9513bSAndrey Smetanin 		return -EINVAL;
732e7d9513bSAndrey Smetanin 
733e7d9513bSAndrey Smetanin 	*pdata = hv->hv_crash_param[index];
734e7d9513bSAndrey Smetanin 	return 0;
735e7d9513bSAndrey Smetanin }
736e7d9513bSAndrey Smetanin 
737e7d9513bSAndrey Smetanin static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata)
738e7d9513bSAndrey Smetanin {
739e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
740e7d9513bSAndrey Smetanin 
741e7d9513bSAndrey Smetanin 	*pdata = hv->hv_crash_ctl;
742e7d9513bSAndrey Smetanin 	return 0;
743e7d9513bSAndrey Smetanin }
744e7d9513bSAndrey Smetanin 
745e7d9513bSAndrey Smetanin static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host)
746e7d9513bSAndrey Smetanin {
747e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
748e7d9513bSAndrey Smetanin 
749e7d9513bSAndrey Smetanin 	if (host)
750e7d9513bSAndrey Smetanin 		hv->hv_crash_ctl = data & HV_X64_MSR_CRASH_CTL_NOTIFY;
751e7d9513bSAndrey Smetanin 
752e7d9513bSAndrey Smetanin 	if (!host && (data & HV_X64_MSR_CRASH_CTL_NOTIFY)) {
753e7d9513bSAndrey Smetanin 
754e7d9513bSAndrey Smetanin 		vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
755e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[0],
756e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[1],
757e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[2],
758e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[3],
759e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[4]);
760e7d9513bSAndrey Smetanin 
761e7d9513bSAndrey Smetanin 		/* Send notification about crash to user space */
762e7d9513bSAndrey Smetanin 		kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
763e7d9513bSAndrey Smetanin 	}
764e7d9513bSAndrey Smetanin 
765e7d9513bSAndrey Smetanin 	return 0;
766e7d9513bSAndrey Smetanin }
767e7d9513bSAndrey Smetanin 
768e7d9513bSAndrey Smetanin static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
769e7d9513bSAndrey Smetanin 				     u32 index, u64 data)
770e7d9513bSAndrey Smetanin {
771e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
772e7d9513bSAndrey Smetanin 
773e7d9513bSAndrey Smetanin 	if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
774e7d9513bSAndrey Smetanin 		return -EINVAL;
775e7d9513bSAndrey Smetanin 
776e7d9513bSAndrey Smetanin 	hv->hv_crash_param[index] = data;
777e7d9513bSAndrey Smetanin 	return 0;
778e7d9513bSAndrey Smetanin }
779e7d9513bSAndrey Smetanin 
780095cf55dSPaolo Bonzini /*
781095cf55dSPaolo Bonzini  * The kvmclock and Hyper-V TSC page use similar formulas, and converting
782095cf55dSPaolo Bonzini  * between them is possible:
783095cf55dSPaolo Bonzini  *
784095cf55dSPaolo Bonzini  * kvmclock formula:
785095cf55dSPaolo Bonzini  *    nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32)
786095cf55dSPaolo Bonzini  *           + system_time
787095cf55dSPaolo Bonzini  *
788095cf55dSPaolo Bonzini  * Hyper-V formula:
789095cf55dSPaolo Bonzini  *    nsec/100 = ticks * scale / 2^64 + offset
790095cf55dSPaolo Bonzini  *
791095cf55dSPaolo Bonzini  * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula.
792095cf55dSPaolo Bonzini  * By dividing the kvmclock formula by 100 and equating what's left we get:
793095cf55dSPaolo Bonzini  *    ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
794095cf55dSPaolo Bonzini  *            scale / 2^64 =         tsc_to_system_mul * 2^(tsc_shift-32) / 100
795095cf55dSPaolo Bonzini  *            scale        =         tsc_to_system_mul * 2^(32+tsc_shift) / 100
796095cf55dSPaolo Bonzini  *
797095cf55dSPaolo Bonzini  * Now expand the kvmclock formula and divide by 100:
798095cf55dSPaolo Bonzini  *    nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32)
799095cf55dSPaolo Bonzini  *           - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32)
800095cf55dSPaolo Bonzini  *           + system_time
801095cf55dSPaolo Bonzini  *    nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
802095cf55dSPaolo Bonzini  *               - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100
803095cf55dSPaolo Bonzini  *               + system_time / 100
804095cf55dSPaolo Bonzini  *
805095cf55dSPaolo Bonzini  * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64:
806095cf55dSPaolo Bonzini  *    nsec/100 = ticks * scale / 2^64
807095cf55dSPaolo Bonzini  *               - tsc_timestamp * scale / 2^64
808095cf55dSPaolo Bonzini  *               + system_time / 100
809095cf55dSPaolo Bonzini  *
810095cf55dSPaolo Bonzini  * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out:
811095cf55dSPaolo Bonzini  *    offset = system_time / 100 - tsc_timestamp * scale / 2^64
812095cf55dSPaolo Bonzini  *
813095cf55dSPaolo Bonzini  * These two equivalencies are implemented in this function.
814095cf55dSPaolo Bonzini  */
815095cf55dSPaolo Bonzini static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock,
816095cf55dSPaolo Bonzini 					HV_REFERENCE_TSC_PAGE *tsc_ref)
817095cf55dSPaolo Bonzini {
818095cf55dSPaolo Bonzini 	u64 max_mul;
819095cf55dSPaolo Bonzini 
820095cf55dSPaolo Bonzini 	if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT))
821095cf55dSPaolo Bonzini 		return false;
822095cf55dSPaolo Bonzini 
823095cf55dSPaolo Bonzini 	/*
824095cf55dSPaolo Bonzini 	 * check if scale would overflow, if so we use the time ref counter
825095cf55dSPaolo Bonzini 	 *    tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64
826095cf55dSPaolo Bonzini 	 *    tsc_to_system_mul / 100 >= 2^(32-tsc_shift)
827095cf55dSPaolo Bonzini 	 *    tsc_to_system_mul >= 100 * 2^(32-tsc_shift)
828095cf55dSPaolo Bonzini 	 */
829095cf55dSPaolo Bonzini 	max_mul = 100ull << (32 - hv_clock->tsc_shift);
830095cf55dSPaolo Bonzini 	if (hv_clock->tsc_to_system_mul >= max_mul)
831095cf55dSPaolo Bonzini 		return false;
832095cf55dSPaolo Bonzini 
833095cf55dSPaolo Bonzini 	/*
834095cf55dSPaolo Bonzini 	 * Otherwise compute the scale and offset according to the formulas
835095cf55dSPaolo Bonzini 	 * derived above.
836095cf55dSPaolo Bonzini 	 */
837095cf55dSPaolo Bonzini 	tsc_ref->tsc_scale =
838095cf55dSPaolo Bonzini 		mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift),
839095cf55dSPaolo Bonzini 				hv_clock->tsc_to_system_mul,
840095cf55dSPaolo Bonzini 				100);
841095cf55dSPaolo Bonzini 
842095cf55dSPaolo Bonzini 	tsc_ref->tsc_offset = hv_clock->system_time;
843095cf55dSPaolo Bonzini 	do_div(tsc_ref->tsc_offset, 100);
844095cf55dSPaolo Bonzini 	tsc_ref->tsc_offset -=
845095cf55dSPaolo Bonzini 		mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64);
846095cf55dSPaolo Bonzini 	return true;
847095cf55dSPaolo Bonzini }
848095cf55dSPaolo Bonzini 
849095cf55dSPaolo Bonzini void kvm_hv_setup_tsc_page(struct kvm *kvm,
850095cf55dSPaolo Bonzini 			   struct pvclock_vcpu_time_info *hv_clock)
851095cf55dSPaolo Bonzini {
852095cf55dSPaolo Bonzini 	struct kvm_hv *hv = &kvm->arch.hyperv;
853095cf55dSPaolo Bonzini 	u32 tsc_seq;
854095cf55dSPaolo Bonzini 	u64 gfn;
855095cf55dSPaolo Bonzini 
856095cf55dSPaolo Bonzini 	BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence));
857095cf55dSPaolo Bonzini 	BUILD_BUG_ON(offsetof(HV_REFERENCE_TSC_PAGE, tsc_sequence) != 0);
858095cf55dSPaolo Bonzini 
859095cf55dSPaolo Bonzini 	if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
860095cf55dSPaolo Bonzini 		return;
861095cf55dSPaolo Bonzini 
8623f5ad8beSPaolo Bonzini 	mutex_lock(&kvm->arch.hyperv.hv_lock);
8633f5ad8beSPaolo Bonzini 	if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
8643f5ad8beSPaolo Bonzini 		goto out_unlock;
8653f5ad8beSPaolo Bonzini 
866095cf55dSPaolo Bonzini 	gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
867095cf55dSPaolo Bonzini 	/*
868095cf55dSPaolo Bonzini 	 * Because the TSC parameters only vary when there is a
869095cf55dSPaolo Bonzini 	 * change in the master clock, do not bother with caching.
870095cf55dSPaolo Bonzini 	 */
871095cf55dSPaolo Bonzini 	if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn),
872095cf55dSPaolo Bonzini 				    &tsc_seq, sizeof(tsc_seq))))
8733f5ad8beSPaolo Bonzini 		goto out_unlock;
874095cf55dSPaolo Bonzini 
875095cf55dSPaolo Bonzini 	/*
876095cf55dSPaolo Bonzini 	 * While we're computing and writing the parameters, force the
877095cf55dSPaolo Bonzini 	 * guest to use the time reference count MSR.
878095cf55dSPaolo Bonzini 	 */
879095cf55dSPaolo Bonzini 	hv->tsc_ref.tsc_sequence = 0;
880095cf55dSPaolo Bonzini 	if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
881095cf55dSPaolo Bonzini 			    &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
8823f5ad8beSPaolo Bonzini 		goto out_unlock;
883095cf55dSPaolo Bonzini 
884095cf55dSPaolo Bonzini 	if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref))
8853f5ad8beSPaolo Bonzini 		goto out_unlock;
886095cf55dSPaolo Bonzini 
887095cf55dSPaolo Bonzini 	/* Ensure sequence is zero before writing the rest of the struct.  */
888095cf55dSPaolo Bonzini 	smp_wmb();
889095cf55dSPaolo Bonzini 	if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
8903f5ad8beSPaolo Bonzini 		goto out_unlock;
891095cf55dSPaolo Bonzini 
892095cf55dSPaolo Bonzini 	/*
893095cf55dSPaolo Bonzini 	 * Now switch to the TSC page mechanism by writing the sequence.
894095cf55dSPaolo Bonzini 	 */
895095cf55dSPaolo Bonzini 	tsc_seq++;
896095cf55dSPaolo Bonzini 	if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0)
897095cf55dSPaolo Bonzini 		tsc_seq = 1;
898095cf55dSPaolo Bonzini 
899095cf55dSPaolo Bonzini 	/* Write the struct entirely before the non-zero sequence.  */
900095cf55dSPaolo Bonzini 	smp_wmb();
901095cf55dSPaolo Bonzini 
902095cf55dSPaolo Bonzini 	hv->tsc_ref.tsc_sequence = tsc_seq;
903095cf55dSPaolo Bonzini 	kvm_write_guest(kvm, gfn_to_gpa(gfn),
904095cf55dSPaolo Bonzini 			&hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence));
9053f5ad8beSPaolo Bonzini out_unlock:
9063f5ad8beSPaolo Bonzini 	mutex_unlock(&kvm->arch.hyperv.hv_lock);
907095cf55dSPaolo Bonzini }
908095cf55dSPaolo Bonzini 
909e7d9513bSAndrey Smetanin static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
910e7d9513bSAndrey Smetanin 			     bool host)
911e83d5887SAndrey Smetanin {
912e83d5887SAndrey Smetanin 	struct kvm *kvm = vcpu->kvm;
913e83d5887SAndrey Smetanin 	struct kvm_hv *hv = &kvm->arch.hyperv;
914e83d5887SAndrey Smetanin 
915e83d5887SAndrey Smetanin 	switch (msr) {
916e83d5887SAndrey Smetanin 	case HV_X64_MSR_GUEST_OS_ID:
917e83d5887SAndrey Smetanin 		hv->hv_guest_os_id = data;
918e83d5887SAndrey Smetanin 		/* setting guest os id to zero disables hypercall page */
919e83d5887SAndrey Smetanin 		if (!hv->hv_guest_os_id)
920e83d5887SAndrey Smetanin 			hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
921e83d5887SAndrey Smetanin 		break;
922e83d5887SAndrey Smetanin 	case HV_X64_MSR_HYPERCALL: {
923e83d5887SAndrey Smetanin 		u64 gfn;
924e83d5887SAndrey Smetanin 		unsigned long addr;
925e83d5887SAndrey Smetanin 		u8 instructions[4];
926e83d5887SAndrey Smetanin 
927e83d5887SAndrey Smetanin 		/* if guest os id is not set hypercall should remain disabled */
928e83d5887SAndrey Smetanin 		if (!hv->hv_guest_os_id)
929e83d5887SAndrey Smetanin 			break;
930e83d5887SAndrey Smetanin 		if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
931e83d5887SAndrey Smetanin 			hv->hv_hypercall = data;
932e83d5887SAndrey Smetanin 			break;
933e83d5887SAndrey Smetanin 		}
934e83d5887SAndrey Smetanin 		gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
935e83d5887SAndrey Smetanin 		addr = gfn_to_hva(kvm, gfn);
936e83d5887SAndrey Smetanin 		if (kvm_is_error_hva(addr))
937e83d5887SAndrey Smetanin 			return 1;
938e83d5887SAndrey Smetanin 		kvm_x86_ops->patch_hypercall(vcpu, instructions);
939e83d5887SAndrey Smetanin 		((unsigned char *)instructions)[3] = 0xc3; /* ret */
940e83d5887SAndrey Smetanin 		if (__copy_to_user((void __user *)addr, instructions, 4))
941e83d5887SAndrey Smetanin 			return 1;
942e83d5887SAndrey Smetanin 		hv->hv_hypercall = data;
943e83d5887SAndrey Smetanin 		mark_page_dirty(kvm, gfn);
944e83d5887SAndrey Smetanin 		break;
945e83d5887SAndrey Smetanin 	}
946095cf55dSPaolo Bonzini 	case HV_X64_MSR_REFERENCE_TSC:
947e83d5887SAndrey Smetanin 		hv->hv_tsc_page = data;
948095cf55dSPaolo Bonzini 		if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)
949095cf55dSPaolo Bonzini 			kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
950e83d5887SAndrey Smetanin 		break;
951e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
952e7d9513bSAndrey Smetanin 		return kvm_hv_msr_set_crash_data(vcpu,
953e7d9513bSAndrey Smetanin 						 msr - HV_X64_MSR_CRASH_P0,
954e7d9513bSAndrey Smetanin 						 data);
955e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_CTL:
956e7d9513bSAndrey Smetanin 		return kvm_hv_msr_set_crash_ctl(vcpu, data, host);
957e516cebbSAndrey Smetanin 	case HV_X64_MSR_RESET:
958e516cebbSAndrey Smetanin 		if (data == 1) {
959e516cebbSAndrey Smetanin 			vcpu_debug(vcpu, "hyper-v reset requested\n");
960e516cebbSAndrey Smetanin 			kvm_make_request(KVM_REQ_HV_RESET, vcpu);
961e516cebbSAndrey Smetanin 		}
962e516cebbSAndrey Smetanin 		break;
963e83d5887SAndrey Smetanin 	default:
964e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
965e83d5887SAndrey Smetanin 			    msr, data);
966e83d5887SAndrey Smetanin 		return 1;
967e83d5887SAndrey Smetanin 	}
968e83d5887SAndrey Smetanin 	return 0;
969e83d5887SAndrey Smetanin }
970e83d5887SAndrey Smetanin 
9719eec50b8SAndrey Smetanin /* Calculate cpu time spent by current task in 100ns units */
9729eec50b8SAndrey Smetanin static u64 current_task_runtime_100ns(void)
9739eec50b8SAndrey Smetanin {
9745613fda9SFrederic Weisbecker 	u64 utime, stime;
9759eec50b8SAndrey Smetanin 
9769eec50b8SAndrey Smetanin 	task_cputime_adjusted(current, &utime, &stime);
9775613fda9SFrederic Weisbecker 
9785613fda9SFrederic Weisbecker 	return div_u64(utime + stime, 100);
9799eec50b8SAndrey Smetanin }
9809eec50b8SAndrey Smetanin 
9819eec50b8SAndrey Smetanin static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
982e83d5887SAndrey Smetanin {
983e83d5887SAndrey Smetanin 	struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
984e83d5887SAndrey Smetanin 
985e83d5887SAndrey Smetanin 	switch (msr) {
986e83d5887SAndrey Smetanin 	case HV_X64_MSR_APIC_ASSIST_PAGE: {
987e83d5887SAndrey Smetanin 		u64 gfn;
988e83d5887SAndrey Smetanin 		unsigned long addr;
989e83d5887SAndrey Smetanin 
990e83d5887SAndrey Smetanin 		if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
991e83d5887SAndrey Smetanin 			hv->hv_vapic = data;
992e83d5887SAndrey Smetanin 			if (kvm_lapic_enable_pv_eoi(vcpu, 0))
993e83d5887SAndrey Smetanin 				return 1;
994e83d5887SAndrey Smetanin 			break;
995e83d5887SAndrey Smetanin 		}
996e83d5887SAndrey Smetanin 		gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT;
997e83d5887SAndrey Smetanin 		addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
998e83d5887SAndrey Smetanin 		if (kvm_is_error_hva(addr))
999e83d5887SAndrey Smetanin 			return 1;
1000e83d5887SAndrey Smetanin 		if (__clear_user((void __user *)addr, PAGE_SIZE))
1001e83d5887SAndrey Smetanin 			return 1;
1002e83d5887SAndrey Smetanin 		hv->hv_vapic = data;
1003e83d5887SAndrey Smetanin 		kvm_vcpu_mark_page_dirty(vcpu, gfn);
1004e83d5887SAndrey Smetanin 		if (kvm_lapic_enable_pv_eoi(vcpu,
1005e83d5887SAndrey Smetanin 					    gfn_to_gpa(gfn) | KVM_MSR_ENABLED))
1006e83d5887SAndrey Smetanin 			return 1;
1007e83d5887SAndrey Smetanin 		break;
1008e83d5887SAndrey Smetanin 	}
1009e83d5887SAndrey Smetanin 	case HV_X64_MSR_EOI:
1010e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1011e83d5887SAndrey Smetanin 	case HV_X64_MSR_ICR:
1012e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1013e83d5887SAndrey Smetanin 	case HV_X64_MSR_TPR:
1014e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
10159eec50b8SAndrey Smetanin 	case HV_X64_MSR_VP_RUNTIME:
10169eec50b8SAndrey Smetanin 		if (!host)
10179eec50b8SAndrey Smetanin 			return 1;
10189eec50b8SAndrey Smetanin 		hv->runtime_offset = data - current_task_runtime_100ns();
10199eec50b8SAndrey Smetanin 		break;
10205c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
10215c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
10225c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
10235c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
10245c919412SAndrey Smetanin 	case HV_X64_MSR_EOM:
10255c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
10265c919412SAndrey Smetanin 		return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host);
10271f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER0_CONFIG:
10281f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER1_CONFIG:
10291f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER2_CONFIG:
10301f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER3_CONFIG: {
10311f4b34f8SAndrey Smetanin 		int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
10321f4b34f8SAndrey Smetanin 
10331f4b34f8SAndrey Smetanin 		return stimer_set_config(vcpu_to_stimer(vcpu, timer_index),
10341f4b34f8SAndrey Smetanin 					 data, host);
10351f4b34f8SAndrey Smetanin 	}
10361f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER0_COUNT:
10371f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER1_COUNT:
10381f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER2_COUNT:
10391f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER3_COUNT: {
10401f4b34f8SAndrey Smetanin 		int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
10411f4b34f8SAndrey Smetanin 
10421f4b34f8SAndrey Smetanin 		return stimer_set_count(vcpu_to_stimer(vcpu, timer_index),
10431f4b34f8SAndrey Smetanin 					data, host);
10441f4b34f8SAndrey Smetanin 	}
1045e83d5887SAndrey Smetanin 	default:
1046e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
1047e83d5887SAndrey Smetanin 			    msr, data);
1048e83d5887SAndrey Smetanin 		return 1;
1049e83d5887SAndrey Smetanin 	}
1050e83d5887SAndrey Smetanin 
1051e83d5887SAndrey Smetanin 	return 0;
1052e83d5887SAndrey Smetanin }
1053e83d5887SAndrey Smetanin 
1054e83d5887SAndrey Smetanin static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1055e83d5887SAndrey Smetanin {
1056e83d5887SAndrey Smetanin 	u64 data = 0;
1057e83d5887SAndrey Smetanin 	struct kvm *kvm = vcpu->kvm;
1058e83d5887SAndrey Smetanin 	struct kvm_hv *hv = &kvm->arch.hyperv;
1059e83d5887SAndrey Smetanin 
1060e83d5887SAndrey Smetanin 	switch (msr) {
1061e83d5887SAndrey Smetanin 	case HV_X64_MSR_GUEST_OS_ID:
1062e83d5887SAndrey Smetanin 		data = hv->hv_guest_os_id;
1063e83d5887SAndrey Smetanin 		break;
1064e83d5887SAndrey Smetanin 	case HV_X64_MSR_HYPERCALL:
1065e83d5887SAndrey Smetanin 		data = hv->hv_hypercall;
1066e83d5887SAndrey Smetanin 		break;
106793bf4172SAndrey Smetanin 	case HV_X64_MSR_TIME_REF_COUNT:
106893bf4172SAndrey Smetanin 		data = get_time_ref_counter(kvm);
1069e83d5887SAndrey Smetanin 		break;
1070e83d5887SAndrey Smetanin 	case HV_X64_MSR_REFERENCE_TSC:
1071e83d5887SAndrey Smetanin 		data = hv->hv_tsc_page;
1072e83d5887SAndrey Smetanin 		break;
1073e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1074e7d9513bSAndrey Smetanin 		return kvm_hv_msr_get_crash_data(vcpu,
1075e7d9513bSAndrey Smetanin 						 msr - HV_X64_MSR_CRASH_P0,
1076e7d9513bSAndrey Smetanin 						 pdata);
1077e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_CTL:
1078e7d9513bSAndrey Smetanin 		return kvm_hv_msr_get_crash_ctl(vcpu, pdata);
1079e516cebbSAndrey Smetanin 	case HV_X64_MSR_RESET:
1080e516cebbSAndrey Smetanin 		data = 0;
1081e516cebbSAndrey Smetanin 		break;
1082e83d5887SAndrey Smetanin 	default:
1083e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1084e83d5887SAndrey Smetanin 		return 1;
1085e83d5887SAndrey Smetanin 	}
1086e83d5887SAndrey Smetanin 
1087e83d5887SAndrey Smetanin 	*pdata = data;
1088e83d5887SAndrey Smetanin 	return 0;
1089e83d5887SAndrey Smetanin }
1090e83d5887SAndrey Smetanin 
1091e83d5887SAndrey Smetanin static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1092e83d5887SAndrey Smetanin {
1093e83d5887SAndrey Smetanin 	u64 data = 0;
1094e83d5887SAndrey Smetanin 	struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
1095e83d5887SAndrey Smetanin 
1096e83d5887SAndrey Smetanin 	switch (msr) {
1097e83d5887SAndrey Smetanin 	case HV_X64_MSR_VP_INDEX: {
1098e83d5887SAndrey Smetanin 		int r;
1099e83d5887SAndrey Smetanin 		struct kvm_vcpu *v;
1100e83d5887SAndrey Smetanin 
1101e83d5887SAndrey Smetanin 		kvm_for_each_vcpu(r, v, vcpu->kvm) {
1102e83d5887SAndrey Smetanin 			if (v == vcpu) {
1103e83d5887SAndrey Smetanin 				data = r;
1104e83d5887SAndrey Smetanin 				break;
1105e83d5887SAndrey Smetanin 			}
1106e83d5887SAndrey Smetanin 		}
1107e83d5887SAndrey Smetanin 		break;
1108e83d5887SAndrey Smetanin 	}
1109e83d5887SAndrey Smetanin 	case HV_X64_MSR_EOI:
1110e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1111e83d5887SAndrey Smetanin 	case HV_X64_MSR_ICR:
1112e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1113e83d5887SAndrey Smetanin 	case HV_X64_MSR_TPR:
1114e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
1115e83d5887SAndrey Smetanin 	case HV_X64_MSR_APIC_ASSIST_PAGE:
1116e83d5887SAndrey Smetanin 		data = hv->hv_vapic;
1117e83d5887SAndrey Smetanin 		break;
11189eec50b8SAndrey Smetanin 	case HV_X64_MSR_VP_RUNTIME:
11199eec50b8SAndrey Smetanin 		data = current_task_runtime_100ns() + hv->runtime_offset;
11209eec50b8SAndrey Smetanin 		break;
11215c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
11225c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
11235c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
11245c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
11255c919412SAndrey Smetanin 	case HV_X64_MSR_EOM:
11265c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
11275c919412SAndrey Smetanin 		return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata);
11281f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER0_CONFIG:
11291f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER1_CONFIG:
11301f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER2_CONFIG:
11311f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER3_CONFIG: {
11321f4b34f8SAndrey Smetanin 		int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
11331f4b34f8SAndrey Smetanin 
11341f4b34f8SAndrey Smetanin 		return stimer_get_config(vcpu_to_stimer(vcpu, timer_index),
11351f4b34f8SAndrey Smetanin 					 pdata);
11361f4b34f8SAndrey Smetanin 	}
11371f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER0_COUNT:
11381f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER1_COUNT:
11391f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER2_COUNT:
11401f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER3_COUNT: {
11411f4b34f8SAndrey Smetanin 		int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
11421f4b34f8SAndrey Smetanin 
11431f4b34f8SAndrey Smetanin 		return stimer_get_count(vcpu_to_stimer(vcpu, timer_index),
11441f4b34f8SAndrey Smetanin 					pdata);
11451f4b34f8SAndrey Smetanin 	}
1146e83d5887SAndrey Smetanin 	default:
1147e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1148e83d5887SAndrey Smetanin 		return 1;
1149e83d5887SAndrey Smetanin 	}
1150e83d5887SAndrey Smetanin 	*pdata = data;
1151e83d5887SAndrey Smetanin 	return 0;
1152e83d5887SAndrey Smetanin }
1153e83d5887SAndrey Smetanin 
1154e7d9513bSAndrey Smetanin int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
1155e83d5887SAndrey Smetanin {
1156e83d5887SAndrey Smetanin 	if (kvm_hv_msr_partition_wide(msr)) {
1157e83d5887SAndrey Smetanin 		int r;
1158e83d5887SAndrey Smetanin 
11593f5ad8beSPaolo Bonzini 		mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
1160e7d9513bSAndrey Smetanin 		r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
11613f5ad8beSPaolo Bonzini 		mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
1162e83d5887SAndrey Smetanin 		return r;
1163e83d5887SAndrey Smetanin 	} else
11649eec50b8SAndrey Smetanin 		return kvm_hv_set_msr(vcpu, msr, data, host);
1165e83d5887SAndrey Smetanin }
1166e83d5887SAndrey Smetanin 
1167e83d5887SAndrey Smetanin int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1168e83d5887SAndrey Smetanin {
1169e83d5887SAndrey Smetanin 	if (kvm_hv_msr_partition_wide(msr)) {
1170e83d5887SAndrey Smetanin 		int r;
1171e83d5887SAndrey Smetanin 
11723f5ad8beSPaolo Bonzini 		mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
1173e83d5887SAndrey Smetanin 		r = kvm_hv_get_msr_pw(vcpu, msr, pdata);
11743f5ad8beSPaolo Bonzini 		mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
1175e83d5887SAndrey Smetanin 		return r;
1176e83d5887SAndrey Smetanin 	} else
1177e83d5887SAndrey Smetanin 		return kvm_hv_get_msr(vcpu, msr, pdata);
1178e83d5887SAndrey Smetanin }
1179e83d5887SAndrey Smetanin 
1180e83d5887SAndrey Smetanin bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1181e83d5887SAndrey Smetanin {
11823f5ad8beSPaolo Bonzini 	return READ_ONCE(kvm->arch.hyperv.hv_hypercall) & HV_X64_MSR_HYPERCALL_ENABLE;
1183e83d5887SAndrey Smetanin }
1184e83d5887SAndrey Smetanin 
118583326e43SAndrey Smetanin static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
118683326e43SAndrey Smetanin {
118783326e43SAndrey Smetanin 	bool longmode;
118883326e43SAndrey Smetanin 
118983326e43SAndrey Smetanin 	longmode = is_64_bit_mode(vcpu);
119083326e43SAndrey Smetanin 	if (longmode)
119183326e43SAndrey Smetanin 		kvm_register_write(vcpu, VCPU_REGS_RAX, result);
119283326e43SAndrey Smetanin 	else {
119383326e43SAndrey Smetanin 		kvm_register_write(vcpu, VCPU_REGS_RDX, result >> 32);
119483326e43SAndrey Smetanin 		kvm_register_write(vcpu, VCPU_REGS_RAX, result & 0xffffffff);
119583326e43SAndrey Smetanin 	}
119683326e43SAndrey Smetanin }
119783326e43SAndrey Smetanin 
119883326e43SAndrey Smetanin static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
119983326e43SAndrey Smetanin {
120083326e43SAndrey Smetanin 	struct kvm_run *run = vcpu->run;
120183326e43SAndrey Smetanin 
120283326e43SAndrey Smetanin 	kvm_hv_hypercall_set_result(vcpu, run->hyperv.u.hcall.result);
120383326e43SAndrey Smetanin 	return 1;
120483326e43SAndrey Smetanin }
120583326e43SAndrey Smetanin 
1206e83d5887SAndrey Smetanin int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
1207e83d5887SAndrey Smetanin {
1208e83d5887SAndrey Smetanin 	u64 param, ingpa, outgpa, ret;
1209e83d5887SAndrey Smetanin 	uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
1210e83d5887SAndrey Smetanin 	bool fast, longmode;
1211e83d5887SAndrey Smetanin 
1212e83d5887SAndrey Smetanin 	/*
1213e83d5887SAndrey Smetanin 	 * hypercall generates UD from non zero cpl and real mode
1214e83d5887SAndrey Smetanin 	 * per HYPER-V spec
1215e83d5887SAndrey Smetanin 	 */
1216e83d5887SAndrey Smetanin 	if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
1217e83d5887SAndrey Smetanin 		kvm_queue_exception(vcpu, UD_VECTOR);
12180d9c055eSAndrey Smetanin 		return 1;
1219e83d5887SAndrey Smetanin 	}
1220e83d5887SAndrey Smetanin 
1221e83d5887SAndrey Smetanin 	longmode = is_64_bit_mode(vcpu);
1222e83d5887SAndrey Smetanin 
1223e83d5887SAndrey Smetanin 	if (!longmode) {
1224e83d5887SAndrey Smetanin 		param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
1225e83d5887SAndrey Smetanin 			(kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
1226e83d5887SAndrey Smetanin 		ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
1227e83d5887SAndrey Smetanin 			(kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
1228e83d5887SAndrey Smetanin 		outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
1229e83d5887SAndrey Smetanin 			(kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
1230e83d5887SAndrey Smetanin 	}
1231e83d5887SAndrey Smetanin #ifdef CONFIG_X86_64
1232e83d5887SAndrey Smetanin 	else {
1233e83d5887SAndrey Smetanin 		param = kvm_register_read(vcpu, VCPU_REGS_RCX);
1234e83d5887SAndrey Smetanin 		ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
1235e83d5887SAndrey Smetanin 		outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
1236e83d5887SAndrey Smetanin 	}
1237e83d5887SAndrey Smetanin #endif
1238e83d5887SAndrey Smetanin 
1239e83d5887SAndrey Smetanin 	code = param & 0xffff;
1240e83d5887SAndrey Smetanin 	fast = (param >> 16) & 0x1;
1241e83d5887SAndrey Smetanin 	rep_cnt = (param >> 32) & 0xfff;
1242e83d5887SAndrey Smetanin 	rep_idx = (param >> 48) & 0xfff;
1243e83d5887SAndrey Smetanin 
1244e83d5887SAndrey Smetanin 	trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
1245e83d5887SAndrey Smetanin 
1246b2fdc257SAndrey Smetanin 	/* Hypercall continuation is not supported yet */
1247b2fdc257SAndrey Smetanin 	if (rep_cnt || rep_idx) {
1248b2fdc257SAndrey Smetanin 		res = HV_STATUS_INVALID_HYPERCALL_CODE;
1249b2fdc257SAndrey Smetanin 		goto set_result;
1250b2fdc257SAndrey Smetanin 	}
1251b2fdc257SAndrey Smetanin 
1252e83d5887SAndrey Smetanin 	switch (code) {
12538ed6d767SAndrey Smetanin 	case HVCALL_NOTIFY_LONG_SPIN_WAIT:
1254e83d5887SAndrey Smetanin 		kvm_vcpu_on_spin(vcpu);
1255e83d5887SAndrey Smetanin 		break;
125683326e43SAndrey Smetanin 	case HVCALL_POST_MESSAGE:
125783326e43SAndrey Smetanin 	case HVCALL_SIGNAL_EVENT:
1258a2b5c3c0SPaolo Bonzini 		/* don't bother userspace if it has no way to handle it */
1259a2b5c3c0SPaolo Bonzini 		if (!vcpu_to_synic(vcpu)->active) {
1260a2b5c3c0SPaolo Bonzini 			res = HV_STATUS_INVALID_HYPERCALL_CODE;
1261a2b5c3c0SPaolo Bonzini 			break;
1262a2b5c3c0SPaolo Bonzini 		}
126383326e43SAndrey Smetanin 		vcpu->run->exit_reason = KVM_EXIT_HYPERV;
126483326e43SAndrey Smetanin 		vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
126583326e43SAndrey Smetanin 		vcpu->run->hyperv.u.hcall.input = param;
126683326e43SAndrey Smetanin 		vcpu->run->hyperv.u.hcall.params[0] = ingpa;
126783326e43SAndrey Smetanin 		vcpu->run->hyperv.u.hcall.params[1] = outgpa;
126883326e43SAndrey Smetanin 		vcpu->arch.complete_userspace_io =
126983326e43SAndrey Smetanin 				kvm_hv_hypercall_complete_userspace;
127083326e43SAndrey Smetanin 		return 0;
1271e83d5887SAndrey Smetanin 	default:
1272e83d5887SAndrey Smetanin 		res = HV_STATUS_INVALID_HYPERCALL_CODE;
1273e83d5887SAndrey Smetanin 		break;
1274e83d5887SAndrey Smetanin 	}
1275e83d5887SAndrey Smetanin 
1276b2fdc257SAndrey Smetanin set_result:
1277e83d5887SAndrey Smetanin 	ret = res | (((u64)rep_done & 0xfff) << 32);
127883326e43SAndrey Smetanin 	kvm_hv_hypercall_set_result(vcpu, ret);
1279e83d5887SAndrey Smetanin 	return 1;
1280e83d5887SAndrey Smetanin }
1281