xref: /openbmc/linux/arch/x86/kvm/hyperv.c (revision 98f65ad458441ea3a243395345811132ee850093)
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>
32faeb7833SRoman Kagan #include <linux/eventfd.h>
3332ef5517SIngo Molnar 
345c919412SAndrey Smetanin #include <asm/apicdef.h>
35e83d5887SAndrey Smetanin #include <trace/events/kvm.h>
36e83d5887SAndrey Smetanin 
37e83d5887SAndrey Smetanin #include "trace.h"
38e83d5887SAndrey Smetanin 
395c919412SAndrey Smetanin static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
405c919412SAndrey Smetanin {
415c919412SAndrey Smetanin 	return atomic64_read(&synic->sint[sint]);
425c919412SAndrey Smetanin }
435c919412SAndrey Smetanin 
445c919412SAndrey Smetanin static inline int synic_get_sint_vector(u64 sint_value)
455c919412SAndrey Smetanin {
465c919412SAndrey Smetanin 	if (sint_value & HV_SYNIC_SINT_MASKED)
475c919412SAndrey Smetanin 		return -1;
485c919412SAndrey Smetanin 	return sint_value & HV_SYNIC_SINT_VECTOR_MASK;
495c919412SAndrey Smetanin }
505c919412SAndrey Smetanin 
515c919412SAndrey Smetanin static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic,
525c919412SAndrey Smetanin 				      int vector)
535c919412SAndrey Smetanin {
545c919412SAndrey Smetanin 	int i;
555c919412SAndrey Smetanin 
565c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
575c919412SAndrey Smetanin 		if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
585c919412SAndrey Smetanin 			return true;
595c919412SAndrey Smetanin 	}
605c919412SAndrey Smetanin 	return false;
615c919412SAndrey Smetanin }
625c919412SAndrey Smetanin 
635c919412SAndrey Smetanin static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic,
645c919412SAndrey Smetanin 				     int vector)
655c919412SAndrey Smetanin {
665c919412SAndrey Smetanin 	int i;
675c919412SAndrey Smetanin 	u64 sint_value;
685c919412SAndrey Smetanin 
695c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
705c919412SAndrey Smetanin 		sint_value = synic_read_sint(synic, i);
715c919412SAndrey Smetanin 		if (synic_get_sint_vector(sint_value) == vector &&
725c919412SAndrey Smetanin 		    sint_value & HV_SYNIC_SINT_AUTO_EOI)
735c919412SAndrey Smetanin 			return true;
745c919412SAndrey Smetanin 	}
755c919412SAndrey Smetanin 	return false;
765c919412SAndrey Smetanin }
775c919412SAndrey Smetanin 
78*98f65ad4SVitaly Kuznetsov static void synic_update_vector(struct kvm_vcpu_hv_synic *synic,
79*98f65ad4SVitaly Kuznetsov 				int vector)
805c919412SAndrey Smetanin {
81*98f65ad4SVitaly Kuznetsov 	if (vector < HV_SYNIC_FIRST_VALID_VECTOR)
82*98f65ad4SVitaly Kuznetsov 		return;
835c919412SAndrey Smetanin 
845c919412SAndrey Smetanin 	if (synic_has_vector_connected(synic, vector))
855c919412SAndrey Smetanin 		__set_bit(vector, synic->vec_bitmap);
865c919412SAndrey Smetanin 	else
875c919412SAndrey Smetanin 		__clear_bit(vector, synic->vec_bitmap);
885c919412SAndrey Smetanin 
895c919412SAndrey Smetanin 	if (synic_has_vector_auto_eoi(synic, vector))
905c919412SAndrey Smetanin 		__set_bit(vector, synic->auto_eoi_bitmap);
915c919412SAndrey Smetanin 	else
925c919412SAndrey Smetanin 		__clear_bit(vector, synic->auto_eoi_bitmap);
93*98f65ad4SVitaly Kuznetsov }
94*98f65ad4SVitaly Kuznetsov 
95*98f65ad4SVitaly Kuznetsov static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint,
96*98f65ad4SVitaly Kuznetsov 			  u64 data, bool host)
97*98f65ad4SVitaly Kuznetsov {
98*98f65ad4SVitaly Kuznetsov 	int vector, old_vector;
99*98f65ad4SVitaly Kuznetsov 
100*98f65ad4SVitaly Kuznetsov 	vector = data & HV_SYNIC_SINT_VECTOR_MASK;
101*98f65ad4SVitaly Kuznetsov 	if (vector < HV_SYNIC_FIRST_VALID_VECTOR && !host)
102*98f65ad4SVitaly Kuznetsov 		return 1;
103*98f65ad4SVitaly Kuznetsov 	/*
104*98f65ad4SVitaly Kuznetsov 	 * Guest may configure multiple SINTs to use the same vector, so
105*98f65ad4SVitaly Kuznetsov 	 * we maintain a bitmap of vectors handled by synic, and a
106*98f65ad4SVitaly Kuznetsov 	 * bitmap of vectors with auto-eoi behavior.  The bitmaps are
107*98f65ad4SVitaly Kuznetsov 	 * updated here, and atomically queried on fast paths.
108*98f65ad4SVitaly Kuznetsov 	 */
109*98f65ad4SVitaly Kuznetsov 	old_vector = synic_read_sint(synic, sint) & HV_SYNIC_SINT_VECTOR_MASK;
110*98f65ad4SVitaly Kuznetsov 
111*98f65ad4SVitaly Kuznetsov 	atomic64_set(&synic->sint[sint], data);
112*98f65ad4SVitaly Kuznetsov 
113*98f65ad4SVitaly Kuznetsov 	synic_update_vector(synic, old_vector);
114*98f65ad4SVitaly Kuznetsov 
115*98f65ad4SVitaly Kuznetsov 	synic_update_vector(synic, vector);
1165c919412SAndrey Smetanin 
1175c919412SAndrey Smetanin 	/* Load SynIC vectors into EOI exit bitmap */
1185c919412SAndrey Smetanin 	kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic));
1195c919412SAndrey Smetanin 	return 0;
1205c919412SAndrey Smetanin }
1215c919412SAndrey Smetanin 
122d3457c87SRoman Kagan static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx)
123d3457c87SRoman Kagan {
124d3457c87SRoman Kagan 	struct kvm_vcpu *vcpu = NULL;
125d3457c87SRoman Kagan 	int i;
126d3457c87SRoman Kagan 
127d3457c87SRoman Kagan 	if (vpidx < KVM_MAX_VCPUS)
128d3457c87SRoman Kagan 		vcpu = kvm_get_vcpu(kvm, vpidx);
129d3457c87SRoman Kagan 	if (vcpu && vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx)
130d3457c87SRoman Kagan 		return vcpu;
131d3457c87SRoman Kagan 	kvm_for_each_vcpu(i, vcpu, kvm)
132d3457c87SRoman Kagan 		if (vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx)
133d3457c87SRoman Kagan 			return vcpu;
134d3457c87SRoman Kagan 	return NULL;
135d3457c87SRoman Kagan }
136d3457c87SRoman Kagan 
137d3457c87SRoman Kagan static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vpidx)
1385c919412SAndrey Smetanin {
1395c919412SAndrey Smetanin 	struct kvm_vcpu *vcpu;
1405c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic;
1415c919412SAndrey Smetanin 
142d3457c87SRoman Kagan 	vcpu = get_vcpu_by_vpidx(kvm, vpidx);
1435c919412SAndrey Smetanin 	if (!vcpu)
1445c919412SAndrey Smetanin 		return NULL;
1455c919412SAndrey Smetanin 	synic = vcpu_to_synic(vcpu);
1465c919412SAndrey Smetanin 	return (synic->active) ? synic : NULL;
1475c919412SAndrey Smetanin }
1485c919412SAndrey Smetanin 
149765eaa0fSAndrey Smetanin static void synic_clear_sint_msg_pending(struct kvm_vcpu_hv_synic *synic,
150765eaa0fSAndrey Smetanin 					u32 sint)
151765eaa0fSAndrey Smetanin {
152765eaa0fSAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
153765eaa0fSAndrey Smetanin 	struct page *page;
154765eaa0fSAndrey Smetanin 	gpa_t gpa;
155765eaa0fSAndrey Smetanin 	struct hv_message *msg;
156765eaa0fSAndrey Smetanin 	struct hv_message_page *msg_page;
157765eaa0fSAndrey Smetanin 
158765eaa0fSAndrey Smetanin 	gpa = synic->msg_page & PAGE_MASK;
159765eaa0fSAndrey Smetanin 	page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
160765eaa0fSAndrey Smetanin 	if (is_error_page(page)) {
161765eaa0fSAndrey Smetanin 		vcpu_err(vcpu, "Hyper-V SynIC can't get msg page, gpa 0x%llx\n",
162765eaa0fSAndrey Smetanin 			 gpa);
163765eaa0fSAndrey Smetanin 		return;
164765eaa0fSAndrey Smetanin 	}
165765eaa0fSAndrey Smetanin 	msg_page = kmap_atomic(page);
166765eaa0fSAndrey Smetanin 
167765eaa0fSAndrey Smetanin 	msg = &msg_page->sint_message[sint];
168765eaa0fSAndrey Smetanin 	msg->header.message_flags.msg_pending = 0;
169765eaa0fSAndrey Smetanin 
170765eaa0fSAndrey Smetanin 	kunmap_atomic(msg_page);
171765eaa0fSAndrey Smetanin 	kvm_release_page_dirty(page);
172765eaa0fSAndrey Smetanin 	kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
173765eaa0fSAndrey Smetanin }
174765eaa0fSAndrey Smetanin 
1755c919412SAndrey Smetanin static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
1765c919412SAndrey Smetanin {
1775c919412SAndrey Smetanin 	struct kvm *kvm = vcpu->kvm;
178765eaa0fSAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
1791f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
1801f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv_stimer *stimer;
1811f4b34f8SAndrey Smetanin 	int gsi, idx, stimers_pending;
1825c919412SAndrey Smetanin 
18318659a9cSAndrey Smetanin 	trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint);
1845c919412SAndrey Smetanin 
185765eaa0fSAndrey Smetanin 	if (synic->msg_page & HV_SYNIC_SIMP_ENABLE)
186765eaa0fSAndrey Smetanin 		synic_clear_sint_msg_pending(synic, sint);
187765eaa0fSAndrey Smetanin 
1881f4b34f8SAndrey Smetanin 	/* Try to deliver pending Hyper-V SynIC timers messages */
1891f4b34f8SAndrey Smetanin 	stimers_pending = 0;
1901f4b34f8SAndrey Smetanin 	for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) {
1911f4b34f8SAndrey Smetanin 		stimer = &hv_vcpu->stimer[idx];
1921f4b34f8SAndrey Smetanin 		if (stimer->msg_pending &&
1931f4b34f8SAndrey Smetanin 		    (stimer->config & HV_STIMER_ENABLE) &&
1941f4b34f8SAndrey Smetanin 		    HV_STIMER_SINT(stimer->config) == sint) {
1951f4b34f8SAndrey Smetanin 			set_bit(stimer->index,
1961f4b34f8SAndrey Smetanin 				hv_vcpu->stimer_pending_bitmap);
1971f4b34f8SAndrey Smetanin 			stimers_pending++;
1981f4b34f8SAndrey Smetanin 		}
1991f4b34f8SAndrey Smetanin 	}
2001f4b34f8SAndrey Smetanin 	if (stimers_pending)
2011f4b34f8SAndrey Smetanin 		kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
2021f4b34f8SAndrey Smetanin 
2035c919412SAndrey Smetanin 	idx = srcu_read_lock(&kvm->irq_srcu);
2041f4b34f8SAndrey Smetanin 	gsi = atomic_read(&synic->sint_to_gsi[sint]);
2055c919412SAndrey Smetanin 	if (gsi != -1)
2065c919412SAndrey Smetanin 		kvm_notify_acked_gsi(kvm, gsi);
2075c919412SAndrey Smetanin 	srcu_read_unlock(&kvm->irq_srcu, idx);
2085c919412SAndrey Smetanin }
2095c919412SAndrey Smetanin 
210db397571SAndrey Smetanin static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
211db397571SAndrey Smetanin {
212db397571SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
213db397571SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
214db397571SAndrey Smetanin 
215db397571SAndrey Smetanin 	hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
216db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.msr = msr;
217db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.control = synic->control;
218db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
219db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
220db397571SAndrey Smetanin 
221db397571SAndrey Smetanin 	kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
222db397571SAndrey Smetanin }
223db397571SAndrey Smetanin 
2245c919412SAndrey Smetanin static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
2255c919412SAndrey Smetanin 			 u32 msr, u64 data, bool host)
2265c919412SAndrey Smetanin {
2275c919412SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
2285c919412SAndrey Smetanin 	int ret;
2295c919412SAndrey Smetanin 
2305c919412SAndrey Smetanin 	if (!synic->active)
2315c919412SAndrey Smetanin 		return 1;
2325c919412SAndrey Smetanin 
23318659a9cSAndrey Smetanin 	trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host);
23418659a9cSAndrey Smetanin 
2355c919412SAndrey Smetanin 	ret = 0;
2365c919412SAndrey Smetanin 	switch (msr) {
2375c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
2385c919412SAndrey Smetanin 		synic->control = data;
239db397571SAndrey Smetanin 		if (!host)
240db397571SAndrey Smetanin 			synic_exit(synic, msr);
2415c919412SAndrey Smetanin 		break;
2425c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
2435c919412SAndrey Smetanin 		if (!host) {
2445c919412SAndrey Smetanin 			ret = 1;
2455c919412SAndrey Smetanin 			break;
2465c919412SAndrey Smetanin 		}
2475c919412SAndrey Smetanin 		synic->version = data;
2485c919412SAndrey Smetanin 		break;
2495c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
250efc479e6SRoman Kagan 		if ((data & HV_SYNIC_SIEFP_ENABLE) && !host &&
251efc479e6SRoman Kagan 		    !synic->dont_zero_synic_pages)
2525c919412SAndrey Smetanin 			if (kvm_clear_guest(vcpu->kvm,
2535c919412SAndrey Smetanin 					    data & PAGE_MASK, PAGE_SIZE)) {
2545c919412SAndrey Smetanin 				ret = 1;
2555c919412SAndrey Smetanin 				break;
2565c919412SAndrey Smetanin 			}
2575c919412SAndrey Smetanin 		synic->evt_page = data;
258db397571SAndrey Smetanin 		if (!host)
259db397571SAndrey Smetanin 			synic_exit(synic, msr);
2605c919412SAndrey Smetanin 		break;
2615c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
262efc479e6SRoman Kagan 		if ((data & HV_SYNIC_SIMP_ENABLE) && !host &&
263efc479e6SRoman Kagan 		    !synic->dont_zero_synic_pages)
2645c919412SAndrey Smetanin 			if (kvm_clear_guest(vcpu->kvm,
2655c919412SAndrey Smetanin 					    data & PAGE_MASK, PAGE_SIZE)) {
2665c919412SAndrey Smetanin 				ret = 1;
2675c919412SAndrey Smetanin 				break;
2685c919412SAndrey Smetanin 			}
2695c919412SAndrey Smetanin 		synic->msg_page = data;
270db397571SAndrey Smetanin 		if (!host)
271db397571SAndrey Smetanin 			synic_exit(synic, msr);
2725c919412SAndrey Smetanin 		break;
2735c919412SAndrey Smetanin 	case HV_X64_MSR_EOM: {
2745c919412SAndrey Smetanin 		int i;
2755c919412SAndrey Smetanin 
2765c919412SAndrey Smetanin 		for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
2775c919412SAndrey Smetanin 			kvm_hv_notify_acked_sint(vcpu, i);
2785c919412SAndrey Smetanin 		break;
2795c919412SAndrey Smetanin 	}
2805c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
2817be58a64SAndrey Smetanin 		ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host);
2825c919412SAndrey Smetanin 		break;
2835c919412SAndrey Smetanin 	default:
2845c919412SAndrey Smetanin 		ret = 1;
2855c919412SAndrey Smetanin 		break;
2865c919412SAndrey Smetanin 	}
2875c919412SAndrey Smetanin 	return ret;
2885c919412SAndrey Smetanin }
2895c919412SAndrey Smetanin 
2905c919412SAndrey Smetanin static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata)
2915c919412SAndrey Smetanin {
2925c919412SAndrey Smetanin 	int ret;
2935c919412SAndrey Smetanin 
2945c919412SAndrey Smetanin 	if (!synic->active)
2955c919412SAndrey Smetanin 		return 1;
2965c919412SAndrey Smetanin 
2975c919412SAndrey Smetanin 	ret = 0;
2985c919412SAndrey Smetanin 	switch (msr) {
2995c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
3005c919412SAndrey Smetanin 		*pdata = synic->control;
3015c919412SAndrey Smetanin 		break;
3025c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
3035c919412SAndrey Smetanin 		*pdata = synic->version;
3045c919412SAndrey Smetanin 		break;
3055c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
3065c919412SAndrey Smetanin 		*pdata = synic->evt_page;
3075c919412SAndrey Smetanin 		break;
3085c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
3095c919412SAndrey Smetanin 		*pdata = synic->msg_page;
3105c919412SAndrey Smetanin 		break;
3115c919412SAndrey Smetanin 	case HV_X64_MSR_EOM:
3125c919412SAndrey Smetanin 		*pdata = 0;
3135c919412SAndrey Smetanin 		break;
3145c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
3155c919412SAndrey Smetanin 		*pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
3165c919412SAndrey Smetanin 		break;
3175c919412SAndrey Smetanin 	default:
3185c919412SAndrey Smetanin 		ret = 1;
3195c919412SAndrey Smetanin 		break;
3205c919412SAndrey Smetanin 	}
3215c919412SAndrey Smetanin 	return ret;
3225c919412SAndrey Smetanin }
3235c919412SAndrey Smetanin 
324ecd8a8c2SJiang Biao static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
3255c919412SAndrey Smetanin {
3265c919412SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
3275c919412SAndrey Smetanin 	struct kvm_lapic_irq irq;
3285c919412SAndrey Smetanin 	int ret, vector;
3295c919412SAndrey Smetanin 
3305c919412SAndrey Smetanin 	if (sint >= ARRAY_SIZE(synic->sint))
3315c919412SAndrey Smetanin 		return -EINVAL;
3325c919412SAndrey Smetanin 
3335c919412SAndrey Smetanin 	vector = synic_get_sint_vector(synic_read_sint(synic, sint));
3345c919412SAndrey Smetanin 	if (vector < 0)
3355c919412SAndrey Smetanin 		return -ENOENT;
3365c919412SAndrey Smetanin 
3375c919412SAndrey Smetanin 	memset(&irq, 0, sizeof(irq));
338f98a3efbSRadim Krčmář 	irq.shorthand = APIC_DEST_SELF;
3395c919412SAndrey Smetanin 	irq.dest_mode = APIC_DEST_PHYSICAL;
3405c919412SAndrey Smetanin 	irq.delivery_mode = APIC_DM_FIXED;
3415c919412SAndrey Smetanin 	irq.vector = vector;
3425c919412SAndrey Smetanin 	irq.level = 1;
3435c919412SAndrey Smetanin 
344f98a3efbSRadim Krčmář 	ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq, NULL);
34518659a9cSAndrey Smetanin 	trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret);
3465c919412SAndrey Smetanin 	return ret;
3475c919412SAndrey Smetanin }
3485c919412SAndrey Smetanin 
349d3457c87SRoman Kagan int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vpidx, u32 sint)
3505c919412SAndrey Smetanin {
3515c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic;
3525c919412SAndrey Smetanin 
353d3457c87SRoman Kagan 	synic = synic_get(kvm, vpidx);
3545c919412SAndrey Smetanin 	if (!synic)
3555c919412SAndrey Smetanin 		return -EINVAL;
3565c919412SAndrey Smetanin 
3575c919412SAndrey Smetanin 	return synic_set_irq(synic, sint);
3585c919412SAndrey Smetanin }
3595c919412SAndrey Smetanin 
3605c919412SAndrey Smetanin void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
3615c919412SAndrey Smetanin {
3625c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
3635c919412SAndrey Smetanin 	int i;
3645c919412SAndrey Smetanin 
36518659a9cSAndrey Smetanin 	trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector);
3665c919412SAndrey Smetanin 
3675c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
3685c919412SAndrey Smetanin 		if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
3695c919412SAndrey Smetanin 			kvm_hv_notify_acked_sint(vcpu, i);
3705c919412SAndrey Smetanin }
3715c919412SAndrey Smetanin 
372d3457c87SRoman Kagan static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vpidx, u32 sint, int gsi)
3735c919412SAndrey Smetanin {
3745c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic;
3755c919412SAndrey Smetanin 
376d3457c87SRoman Kagan 	synic = synic_get(kvm, vpidx);
3775c919412SAndrey Smetanin 	if (!synic)
3785c919412SAndrey Smetanin 		return -EINVAL;
3795c919412SAndrey Smetanin 
3805c919412SAndrey Smetanin 	if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
3815c919412SAndrey Smetanin 		return -EINVAL;
3825c919412SAndrey Smetanin 
3835c919412SAndrey Smetanin 	atomic_set(&synic->sint_to_gsi[sint], gsi);
3845c919412SAndrey Smetanin 	return 0;
3855c919412SAndrey Smetanin }
3865c919412SAndrey Smetanin 
3875c919412SAndrey Smetanin void kvm_hv_irq_routing_update(struct kvm *kvm)
3885c919412SAndrey Smetanin {
3895c919412SAndrey Smetanin 	struct kvm_irq_routing_table *irq_rt;
3905c919412SAndrey Smetanin 	struct kvm_kernel_irq_routing_entry *e;
3915c919412SAndrey Smetanin 	u32 gsi;
3925c919412SAndrey Smetanin 
3935c919412SAndrey Smetanin 	irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
3945c919412SAndrey Smetanin 					lockdep_is_held(&kvm->irq_lock));
3955c919412SAndrey Smetanin 
3965c919412SAndrey Smetanin 	for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
3975c919412SAndrey Smetanin 		hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
3985c919412SAndrey Smetanin 			if (e->type == KVM_IRQ_ROUTING_HV_SINT)
3995c919412SAndrey Smetanin 				kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
4005c919412SAndrey Smetanin 						    e->hv_sint.sint, gsi);
4015c919412SAndrey Smetanin 		}
4025c919412SAndrey Smetanin 	}
4035c919412SAndrey Smetanin }
4045c919412SAndrey Smetanin 
4055c919412SAndrey Smetanin static void synic_init(struct kvm_vcpu_hv_synic *synic)
4065c919412SAndrey Smetanin {
4075c919412SAndrey Smetanin 	int i;
4085c919412SAndrey Smetanin 
4095c919412SAndrey Smetanin 	memset(synic, 0, sizeof(*synic));
4105c919412SAndrey Smetanin 	synic->version = HV_SYNIC_VERSION_1;
4115c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
4125c919412SAndrey Smetanin 		atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
4135c919412SAndrey Smetanin 		atomic_set(&synic->sint_to_gsi[i], -1);
4145c919412SAndrey Smetanin 	}
4155c919412SAndrey Smetanin }
4165c919412SAndrey Smetanin 
41793bf4172SAndrey Smetanin static u64 get_time_ref_counter(struct kvm *kvm)
41893bf4172SAndrey Smetanin {
419095cf55dSPaolo Bonzini 	struct kvm_hv *hv = &kvm->arch.hyperv;
420095cf55dSPaolo Bonzini 	struct kvm_vcpu *vcpu;
421095cf55dSPaolo Bonzini 	u64 tsc;
422095cf55dSPaolo Bonzini 
423095cf55dSPaolo Bonzini 	/*
424095cf55dSPaolo Bonzini 	 * The guest has not set up the TSC page or the clock isn't
425095cf55dSPaolo Bonzini 	 * stable, fall back to get_kvmclock_ns.
426095cf55dSPaolo Bonzini 	 */
427095cf55dSPaolo Bonzini 	if (!hv->tsc_ref.tsc_sequence)
428108b249cSPaolo Bonzini 		return div_u64(get_kvmclock_ns(kvm), 100);
429095cf55dSPaolo Bonzini 
430095cf55dSPaolo Bonzini 	vcpu = kvm_get_vcpu(kvm, 0);
431095cf55dSPaolo Bonzini 	tsc = kvm_read_l1_tsc(vcpu, rdtsc());
432095cf55dSPaolo Bonzini 	return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64)
433095cf55dSPaolo Bonzini 		+ hv->tsc_ref.tsc_offset;
43493bf4172SAndrey Smetanin }
43593bf4172SAndrey Smetanin 
436f3b138c5SAndrey Smetanin static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
4371f4b34f8SAndrey Smetanin 				bool vcpu_kick)
4381f4b34f8SAndrey Smetanin {
4391f4b34f8SAndrey Smetanin 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
4401f4b34f8SAndrey Smetanin 
4411f4b34f8SAndrey Smetanin 	set_bit(stimer->index,
4421f4b34f8SAndrey Smetanin 		vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
4431f4b34f8SAndrey Smetanin 	kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
4441f4b34f8SAndrey Smetanin 	if (vcpu_kick)
4451f4b34f8SAndrey Smetanin 		kvm_vcpu_kick(vcpu);
4461f4b34f8SAndrey Smetanin }
4471f4b34f8SAndrey Smetanin 
4481f4b34f8SAndrey Smetanin static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
4491f4b34f8SAndrey Smetanin {
4501f4b34f8SAndrey Smetanin 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
4511f4b34f8SAndrey Smetanin 
452ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_cleanup(stimer_to_vcpu(stimer)->vcpu_id,
453ac3e5fcaSAndrey Smetanin 				    stimer->index);
454ac3e5fcaSAndrey Smetanin 
455019b9781SAndrey Smetanin 	hrtimer_cancel(&stimer->timer);
4561f4b34f8SAndrey Smetanin 	clear_bit(stimer->index,
4571f4b34f8SAndrey Smetanin 		  vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
4581f4b34f8SAndrey Smetanin 	stimer->msg_pending = false;
459f808495dSAndrey Smetanin 	stimer->exp_time = 0;
4601f4b34f8SAndrey Smetanin }
4611f4b34f8SAndrey Smetanin 
4621f4b34f8SAndrey Smetanin static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
4631f4b34f8SAndrey Smetanin {
4641f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv_stimer *stimer;
4651f4b34f8SAndrey Smetanin 
4661f4b34f8SAndrey Smetanin 	stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
467ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_callback(stimer_to_vcpu(stimer)->vcpu_id,
468ac3e5fcaSAndrey Smetanin 				     stimer->index);
469f3b138c5SAndrey Smetanin 	stimer_mark_pending(stimer, true);
4701f4b34f8SAndrey Smetanin 
4711f4b34f8SAndrey Smetanin 	return HRTIMER_NORESTART;
4721f4b34f8SAndrey Smetanin }
4731f4b34f8SAndrey Smetanin 
474f808495dSAndrey Smetanin /*
475f808495dSAndrey Smetanin  * stimer_start() assumptions:
476f808495dSAndrey Smetanin  * a) stimer->count is not equal to 0
477f808495dSAndrey Smetanin  * b) stimer->config has HV_STIMER_ENABLE flag
478f808495dSAndrey Smetanin  */
4791f4b34f8SAndrey Smetanin static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
4801f4b34f8SAndrey Smetanin {
4811f4b34f8SAndrey Smetanin 	u64 time_now;
4821f4b34f8SAndrey Smetanin 	ktime_t ktime_now;
4831f4b34f8SAndrey Smetanin 
4841f4b34f8SAndrey Smetanin 	time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm);
4851f4b34f8SAndrey Smetanin 	ktime_now = ktime_get();
4861f4b34f8SAndrey Smetanin 
4871f4b34f8SAndrey Smetanin 	if (stimer->config & HV_STIMER_PERIODIC) {
488f808495dSAndrey Smetanin 		if (stimer->exp_time) {
489f808495dSAndrey Smetanin 			if (time_now >= stimer->exp_time) {
490f808495dSAndrey Smetanin 				u64 remainder;
4911f4b34f8SAndrey Smetanin 
492f808495dSAndrey Smetanin 				div64_u64_rem(time_now - stimer->exp_time,
493f808495dSAndrey Smetanin 					      stimer->count, &remainder);
494f808495dSAndrey Smetanin 				stimer->exp_time =
495f808495dSAndrey Smetanin 					time_now + (stimer->count - remainder);
496f808495dSAndrey Smetanin 			}
497f808495dSAndrey Smetanin 		} else
4981f4b34f8SAndrey Smetanin 			stimer->exp_time = time_now + stimer->count;
499f808495dSAndrey Smetanin 
500ac3e5fcaSAndrey Smetanin 		trace_kvm_hv_stimer_start_periodic(
501ac3e5fcaSAndrey Smetanin 					stimer_to_vcpu(stimer)->vcpu_id,
502ac3e5fcaSAndrey Smetanin 					stimer->index,
503ac3e5fcaSAndrey Smetanin 					time_now, stimer->exp_time);
504ac3e5fcaSAndrey Smetanin 
5051f4b34f8SAndrey Smetanin 		hrtimer_start(&stimer->timer,
506f808495dSAndrey Smetanin 			      ktime_add_ns(ktime_now,
507f808495dSAndrey Smetanin 					   100 * (stimer->exp_time - time_now)),
5081f4b34f8SAndrey Smetanin 			      HRTIMER_MODE_ABS);
5091f4b34f8SAndrey Smetanin 		return 0;
5101f4b34f8SAndrey Smetanin 	}
5111f4b34f8SAndrey Smetanin 	stimer->exp_time = stimer->count;
5121f4b34f8SAndrey Smetanin 	if (time_now >= stimer->count) {
5131f4b34f8SAndrey Smetanin 		/*
5141f4b34f8SAndrey Smetanin 		 * Expire timer according to Hypervisor Top-Level Functional
5151f4b34f8SAndrey Smetanin 		 * specification v4(15.3.1):
5161f4b34f8SAndrey Smetanin 		 * "If a one shot is enabled and the specified count is in
5171f4b34f8SAndrey Smetanin 		 * the past, it will expire immediately."
5181f4b34f8SAndrey Smetanin 		 */
519f3b138c5SAndrey Smetanin 		stimer_mark_pending(stimer, false);
5201f4b34f8SAndrey Smetanin 		return 0;
5211f4b34f8SAndrey Smetanin 	}
5221f4b34f8SAndrey Smetanin 
523ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_start_one_shot(stimer_to_vcpu(stimer)->vcpu_id,
524ac3e5fcaSAndrey Smetanin 					   stimer->index,
525ac3e5fcaSAndrey Smetanin 					   time_now, stimer->count);
526ac3e5fcaSAndrey Smetanin 
5271f4b34f8SAndrey Smetanin 	hrtimer_start(&stimer->timer,
5281f4b34f8SAndrey Smetanin 		      ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
5291f4b34f8SAndrey Smetanin 		      HRTIMER_MODE_ABS);
5301f4b34f8SAndrey Smetanin 	return 0;
5311f4b34f8SAndrey Smetanin }
5321f4b34f8SAndrey Smetanin 
5331f4b34f8SAndrey Smetanin static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
5341f4b34f8SAndrey Smetanin 			     bool host)
5351f4b34f8SAndrey Smetanin {
536ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_set_config(stimer_to_vcpu(stimer)->vcpu_id,
537ac3e5fcaSAndrey Smetanin 				       stimer->index, config, host);
538ac3e5fcaSAndrey Smetanin 
539f3b138c5SAndrey Smetanin 	stimer_cleanup(stimer);
54023a3b201SAndrey Smetanin 	if ((stimer->config & HV_STIMER_ENABLE) && HV_STIMER_SINT(config) == 0)
5411f4b34f8SAndrey Smetanin 		config &= ~HV_STIMER_ENABLE;
5421f4b34f8SAndrey Smetanin 	stimer->config = config;
543f3b138c5SAndrey Smetanin 	stimer_mark_pending(stimer, false);
5441f4b34f8SAndrey Smetanin 	return 0;
5451f4b34f8SAndrey Smetanin }
5461f4b34f8SAndrey Smetanin 
5471f4b34f8SAndrey Smetanin static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
5481f4b34f8SAndrey Smetanin 			    bool host)
5491f4b34f8SAndrey Smetanin {
550ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_set_count(stimer_to_vcpu(stimer)->vcpu_id,
551ac3e5fcaSAndrey Smetanin 				      stimer->index, count, host);
552ac3e5fcaSAndrey Smetanin 
5531f4b34f8SAndrey Smetanin 	stimer_cleanup(stimer);
554f3b138c5SAndrey Smetanin 	stimer->count = count;
5551f4b34f8SAndrey Smetanin 	if (stimer->count == 0)
5561f4b34f8SAndrey Smetanin 		stimer->config &= ~HV_STIMER_ENABLE;
557f3b138c5SAndrey Smetanin 	else if (stimer->config & HV_STIMER_AUTOENABLE)
5581f4b34f8SAndrey Smetanin 		stimer->config |= HV_STIMER_ENABLE;
559f3b138c5SAndrey Smetanin 	stimer_mark_pending(stimer, false);
5601f4b34f8SAndrey Smetanin 	return 0;
5611f4b34f8SAndrey Smetanin }
5621f4b34f8SAndrey Smetanin 
5631f4b34f8SAndrey Smetanin static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
5641f4b34f8SAndrey Smetanin {
5651f4b34f8SAndrey Smetanin 	*pconfig = stimer->config;
5661f4b34f8SAndrey Smetanin 	return 0;
5671f4b34f8SAndrey Smetanin }
5681f4b34f8SAndrey Smetanin 
5691f4b34f8SAndrey Smetanin static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
5701f4b34f8SAndrey Smetanin {
5711f4b34f8SAndrey Smetanin 	*pcount = stimer->count;
5721f4b34f8SAndrey Smetanin 	return 0;
5731f4b34f8SAndrey Smetanin }
5741f4b34f8SAndrey Smetanin 
5751f4b34f8SAndrey Smetanin static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
5761f4b34f8SAndrey Smetanin 			     struct hv_message *src_msg)
5771f4b34f8SAndrey Smetanin {
5781f4b34f8SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
5791f4b34f8SAndrey Smetanin 	struct page *page;
5801f4b34f8SAndrey Smetanin 	gpa_t gpa;
5811f4b34f8SAndrey Smetanin 	struct hv_message *dst_msg;
5821f4b34f8SAndrey Smetanin 	int r;
5831f4b34f8SAndrey Smetanin 	struct hv_message_page *msg_page;
5841f4b34f8SAndrey Smetanin 
5851f4b34f8SAndrey Smetanin 	if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
5861f4b34f8SAndrey Smetanin 		return -ENOENT;
5871f4b34f8SAndrey Smetanin 
5881f4b34f8SAndrey Smetanin 	gpa = synic->msg_page & PAGE_MASK;
5891f4b34f8SAndrey Smetanin 	page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
5901f4b34f8SAndrey Smetanin 	if (is_error_page(page))
5911f4b34f8SAndrey Smetanin 		return -EFAULT;
5921f4b34f8SAndrey Smetanin 
5931f4b34f8SAndrey Smetanin 	msg_page = kmap_atomic(page);
5941f4b34f8SAndrey Smetanin 	dst_msg = &msg_page->sint_message[sint];
5951f4b34f8SAndrey Smetanin 	if (sync_cmpxchg(&dst_msg->header.message_type, HVMSG_NONE,
5961f4b34f8SAndrey Smetanin 			 src_msg->header.message_type) != HVMSG_NONE) {
5971f4b34f8SAndrey Smetanin 		dst_msg->header.message_flags.msg_pending = 1;
5981f4b34f8SAndrey Smetanin 		r = -EAGAIN;
5991f4b34f8SAndrey Smetanin 	} else {
6001f4b34f8SAndrey Smetanin 		memcpy(&dst_msg->u.payload, &src_msg->u.payload,
6011f4b34f8SAndrey Smetanin 		       src_msg->header.payload_size);
6021f4b34f8SAndrey Smetanin 		dst_msg->header.message_type = src_msg->header.message_type;
6031f4b34f8SAndrey Smetanin 		dst_msg->header.payload_size = src_msg->header.payload_size;
6041f4b34f8SAndrey Smetanin 		r = synic_set_irq(synic, sint);
6051f4b34f8SAndrey Smetanin 		if (r >= 1)
6061f4b34f8SAndrey Smetanin 			r = 0;
6071f4b34f8SAndrey Smetanin 		else if (r == 0)
6081f4b34f8SAndrey Smetanin 			r = -EFAULT;
6091f4b34f8SAndrey Smetanin 	}
6101f4b34f8SAndrey Smetanin 	kunmap_atomic(msg_page);
6111f4b34f8SAndrey Smetanin 	kvm_release_page_dirty(page);
6121f4b34f8SAndrey Smetanin 	kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
6131f4b34f8SAndrey Smetanin 	return r;
6141f4b34f8SAndrey Smetanin }
6151f4b34f8SAndrey Smetanin 
6160cdeabb1SAndrey Smetanin static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
6171f4b34f8SAndrey Smetanin {
6181f4b34f8SAndrey Smetanin 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
6191f4b34f8SAndrey Smetanin 	struct hv_message *msg = &stimer->msg;
6201f4b34f8SAndrey Smetanin 	struct hv_timer_message_payload *payload =
6211f4b34f8SAndrey Smetanin 			(struct hv_timer_message_payload *)&msg->u.payload;
6221f4b34f8SAndrey Smetanin 
6231f4b34f8SAndrey Smetanin 	payload->expiration_time = stimer->exp_time;
6241f4b34f8SAndrey Smetanin 	payload->delivery_time = get_time_ref_counter(vcpu->kvm);
6250cdeabb1SAndrey Smetanin 	return synic_deliver_msg(vcpu_to_synic(vcpu),
6261f4b34f8SAndrey Smetanin 				 HV_STIMER_SINT(stimer->config), msg);
6271f4b34f8SAndrey Smetanin }
6281f4b34f8SAndrey Smetanin 
6291f4b34f8SAndrey Smetanin static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
6301f4b34f8SAndrey Smetanin {
631ac3e5fcaSAndrey Smetanin 	int r;
632ac3e5fcaSAndrey Smetanin 
6330cdeabb1SAndrey Smetanin 	stimer->msg_pending = true;
634ac3e5fcaSAndrey Smetanin 	r = stimer_send_msg(stimer);
635ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_expiration(stimer_to_vcpu(stimer)->vcpu_id,
636ac3e5fcaSAndrey Smetanin 				       stimer->index, r);
637ac3e5fcaSAndrey Smetanin 	if (!r) {
6380cdeabb1SAndrey Smetanin 		stimer->msg_pending = false;
6391f4b34f8SAndrey Smetanin 		if (!(stimer->config & HV_STIMER_PERIODIC))
6401ac1b65aSAndrey Smetanin 			stimer->config &= ~HV_STIMER_ENABLE;
6410cdeabb1SAndrey Smetanin 	}
6421f4b34f8SAndrey Smetanin }
6431f4b34f8SAndrey Smetanin 
6441f4b34f8SAndrey Smetanin void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
6451f4b34f8SAndrey Smetanin {
6461f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
6471f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv_stimer *stimer;
648f3b138c5SAndrey Smetanin 	u64 time_now, exp_time;
6491f4b34f8SAndrey Smetanin 	int i;
6501f4b34f8SAndrey Smetanin 
6511f4b34f8SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
6521f4b34f8SAndrey Smetanin 		if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
6531f4b34f8SAndrey Smetanin 			stimer = &hv_vcpu->stimer[i];
6541f4b34f8SAndrey Smetanin 			if (stimer->config & HV_STIMER_ENABLE) {
655f3b138c5SAndrey Smetanin 				exp_time = stimer->exp_time;
6560cdeabb1SAndrey Smetanin 
657f3b138c5SAndrey Smetanin 				if (exp_time) {
658f3b138c5SAndrey Smetanin 					time_now =
659f3b138c5SAndrey Smetanin 						get_time_ref_counter(vcpu->kvm);
660f3b138c5SAndrey Smetanin 					if (time_now >= exp_time)
661f3b138c5SAndrey Smetanin 						stimer_expiration(stimer);
662f3b138c5SAndrey Smetanin 				}
663f3b138c5SAndrey Smetanin 
664f3b138c5SAndrey Smetanin 				if ((stimer->config & HV_STIMER_ENABLE) &&
665f1ff89ecSRoman Kagan 				    stimer->count) {
666f1ff89ecSRoman Kagan 					if (!stimer->msg_pending)
6670cdeabb1SAndrey Smetanin 						stimer_start(stimer);
668f1ff89ecSRoman Kagan 				} else
6690cdeabb1SAndrey Smetanin 					stimer_cleanup(stimer);
6701f4b34f8SAndrey Smetanin 			}
6711f4b34f8SAndrey Smetanin 		}
6721f4b34f8SAndrey Smetanin }
6731f4b34f8SAndrey Smetanin 
6741f4b34f8SAndrey Smetanin void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)
6751f4b34f8SAndrey Smetanin {
6761f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
6771f4b34f8SAndrey Smetanin 	int i;
6781f4b34f8SAndrey Smetanin 
6791f4b34f8SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
6801f4b34f8SAndrey Smetanin 		stimer_cleanup(&hv_vcpu->stimer[i]);
6811f4b34f8SAndrey Smetanin }
6821f4b34f8SAndrey Smetanin 
6831f4b34f8SAndrey Smetanin static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer)
6841f4b34f8SAndrey Smetanin {
6851f4b34f8SAndrey Smetanin 	struct hv_message *msg = &stimer->msg;
6861f4b34f8SAndrey Smetanin 	struct hv_timer_message_payload *payload =
6871f4b34f8SAndrey Smetanin 			(struct hv_timer_message_payload *)&msg->u.payload;
6881f4b34f8SAndrey Smetanin 
6891f4b34f8SAndrey Smetanin 	memset(&msg->header, 0, sizeof(msg->header));
6901f4b34f8SAndrey Smetanin 	msg->header.message_type = HVMSG_TIMER_EXPIRED;
6911f4b34f8SAndrey Smetanin 	msg->header.payload_size = sizeof(*payload);
6921f4b34f8SAndrey Smetanin 
6931f4b34f8SAndrey Smetanin 	payload->timer_index = stimer->index;
6941f4b34f8SAndrey Smetanin 	payload->expiration_time = 0;
6951f4b34f8SAndrey Smetanin 	payload->delivery_time = 0;
6961f4b34f8SAndrey Smetanin }
6971f4b34f8SAndrey Smetanin 
6981f4b34f8SAndrey Smetanin static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index)
6991f4b34f8SAndrey Smetanin {
7001f4b34f8SAndrey Smetanin 	memset(stimer, 0, sizeof(*stimer));
7011f4b34f8SAndrey Smetanin 	stimer->index = timer_index;
7021f4b34f8SAndrey Smetanin 	hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
7031f4b34f8SAndrey Smetanin 	stimer->timer.function = stimer_timer_callback;
7041f4b34f8SAndrey Smetanin 	stimer_prepare_msg(stimer);
7051f4b34f8SAndrey Smetanin }
7061f4b34f8SAndrey Smetanin 
7075c919412SAndrey Smetanin void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
7085c919412SAndrey Smetanin {
7091f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
7101f4b34f8SAndrey Smetanin 	int i;
7111f4b34f8SAndrey Smetanin 
7121f4b34f8SAndrey Smetanin 	synic_init(&hv_vcpu->synic);
7131f4b34f8SAndrey Smetanin 
7141f4b34f8SAndrey Smetanin 	bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
7151f4b34f8SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
7161f4b34f8SAndrey Smetanin 		stimer_init(&hv_vcpu->stimer[i], i);
7175c919412SAndrey Smetanin }
7185c919412SAndrey Smetanin 
719d3457c87SRoman Kagan void kvm_hv_vcpu_postcreate(struct kvm_vcpu *vcpu)
720d3457c87SRoman Kagan {
721d3457c87SRoman Kagan 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
722d3457c87SRoman Kagan 
723d3457c87SRoman Kagan 	hv_vcpu->vp_index = kvm_vcpu_get_idx(vcpu);
724d3457c87SRoman Kagan }
725d3457c87SRoman Kagan 
726efc479e6SRoman Kagan int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages)
7275c919412SAndrey Smetanin {
728efc479e6SRoman Kagan 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
729efc479e6SRoman Kagan 
7305c919412SAndrey Smetanin 	/*
7315c919412SAndrey Smetanin 	 * Hyper-V SynIC auto EOI SINT's are
7325c919412SAndrey Smetanin 	 * not compatible with APICV, so deactivate APICV
7335c919412SAndrey Smetanin 	 */
7345c919412SAndrey Smetanin 	kvm_vcpu_deactivate_apicv(vcpu);
735efc479e6SRoman Kagan 	synic->active = true;
736efc479e6SRoman Kagan 	synic->dont_zero_synic_pages = dont_zero_synic_pages;
7375c919412SAndrey Smetanin 	return 0;
7385c919412SAndrey Smetanin }
7395c919412SAndrey Smetanin 
740e83d5887SAndrey Smetanin static bool kvm_hv_msr_partition_wide(u32 msr)
741e83d5887SAndrey Smetanin {
742e83d5887SAndrey Smetanin 	bool r = false;
743e83d5887SAndrey Smetanin 
744e83d5887SAndrey Smetanin 	switch (msr) {
745e83d5887SAndrey Smetanin 	case HV_X64_MSR_GUEST_OS_ID:
746e83d5887SAndrey Smetanin 	case HV_X64_MSR_HYPERCALL:
747e83d5887SAndrey Smetanin 	case HV_X64_MSR_REFERENCE_TSC:
748e83d5887SAndrey Smetanin 	case HV_X64_MSR_TIME_REF_COUNT:
749e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_CTL:
750e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
751e516cebbSAndrey Smetanin 	case HV_X64_MSR_RESET:
752a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
753a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_TSC_EMULATION_CONTROL:
754a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_TSC_EMULATION_STATUS:
755e83d5887SAndrey Smetanin 		r = true;
756e83d5887SAndrey Smetanin 		break;
757e83d5887SAndrey Smetanin 	}
758e83d5887SAndrey Smetanin 
759e83d5887SAndrey Smetanin 	return r;
760e83d5887SAndrey Smetanin }
761e83d5887SAndrey Smetanin 
762e7d9513bSAndrey Smetanin static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
763e7d9513bSAndrey Smetanin 				     u32 index, u64 *pdata)
764e7d9513bSAndrey Smetanin {
765e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
766e7d9513bSAndrey Smetanin 
767e7d9513bSAndrey Smetanin 	if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
768e7d9513bSAndrey Smetanin 		return -EINVAL;
769e7d9513bSAndrey Smetanin 
770e7d9513bSAndrey Smetanin 	*pdata = hv->hv_crash_param[index];
771e7d9513bSAndrey Smetanin 	return 0;
772e7d9513bSAndrey Smetanin }
773e7d9513bSAndrey Smetanin 
774e7d9513bSAndrey Smetanin static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata)
775e7d9513bSAndrey Smetanin {
776e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
777e7d9513bSAndrey Smetanin 
778e7d9513bSAndrey Smetanin 	*pdata = hv->hv_crash_ctl;
779e7d9513bSAndrey Smetanin 	return 0;
780e7d9513bSAndrey Smetanin }
781e7d9513bSAndrey Smetanin 
782e7d9513bSAndrey Smetanin static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host)
783e7d9513bSAndrey Smetanin {
784e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
785e7d9513bSAndrey Smetanin 
786e7d9513bSAndrey Smetanin 	if (host)
787e7d9513bSAndrey Smetanin 		hv->hv_crash_ctl = data & HV_X64_MSR_CRASH_CTL_NOTIFY;
788e7d9513bSAndrey Smetanin 
789e7d9513bSAndrey Smetanin 	if (!host && (data & HV_X64_MSR_CRASH_CTL_NOTIFY)) {
790e7d9513bSAndrey Smetanin 
791e7d9513bSAndrey Smetanin 		vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
792e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[0],
793e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[1],
794e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[2],
795e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[3],
796e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[4]);
797e7d9513bSAndrey Smetanin 
798e7d9513bSAndrey Smetanin 		/* Send notification about crash to user space */
799e7d9513bSAndrey Smetanin 		kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
800e7d9513bSAndrey Smetanin 	}
801e7d9513bSAndrey Smetanin 
802e7d9513bSAndrey Smetanin 	return 0;
803e7d9513bSAndrey Smetanin }
804e7d9513bSAndrey Smetanin 
805e7d9513bSAndrey Smetanin static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
806e7d9513bSAndrey Smetanin 				     u32 index, u64 data)
807e7d9513bSAndrey Smetanin {
808e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
809e7d9513bSAndrey Smetanin 
810e7d9513bSAndrey Smetanin 	if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
811e7d9513bSAndrey Smetanin 		return -EINVAL;
812e7d9513bSAndrey Smetanin 
813e7d9513bSAndrey Smetanin 	hv->hv_crash_param[index] = data;
814e7d9513bSAndrey Smetanin 	return 0;
815e7d9513bSAndrey Smetanin }
816e7d9513bSAndrey Smetanin 
817095cf55dSPaolo Bonzini /*
818095cf55dSPaolo Bonzini  * The kvmclock and Hyper-V TSC page use similar formulas, and converting
819095cf55dSPaolo Bonzini  * between them is possible:
820095cf55dSPaolo Bonzini  *
821095cf55dSPaolo Bonzini  * kvmclock formula:
822095cf55dSPaolo Bonzini  *    nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32)
823095cf55dSPaolo Bonzini  *           + system_time
824095cf55dSPaolo Bonzini  *
825095cf55dSPaolo Bonzini  * Hyper-V formula:
826095cf55dSPaolo Bonzini  *    nsec/100 = ticks * scale / 2^64 + offset
827095cf55dSPaolo Bonzini  *
828095cf55dSPaolo Bonzini  * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula.
829095cf55dSPaolo Bonzini  * By dividing the kvmclock formula by 100 and equating what's left we get:
830095cf55dSPaolo Bonzini  *    ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
831095cf55dSPaolo Bonzini  *            scale / 2^64 =         tsc_to_system_mul * 2^(tsc_shift-32) / 100
832095cf55dSPaolo Bonzini  *            scale        =         tsc_to_system_mul * 2^(32+tsc_shift) / 100
833095cf55dSPaolo Bonzini  *
834095cf55dSPaolo Bonzini  * Now expand the kvmclock formula and divide by 100:
835095cf55dSPaolo Bonzini  *    nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32)
836095cf55dSPaolo Bonzini  *           - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32)
837095cf55dSPaolo Bonzini  *           + system_time
838095cf55dSPaolo Bonzini  *    nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
839095cf55dSPaolo Bonzini  *               - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100
840095cf55dSPaolo Bonzini  *               + system_time / 100
841095cf55dSPaolo Bonzini  *
842095cf55dSPaolo Bonzini  * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64:
843095cf55dSPaolo Bonzini  *    nsec/100 = ticks * scale / 2^64
844095cf55dSPaolo Bonzini  *               - tsc_timestamp * scale / 2^64
845095cf55dSPaolo Bonzini  *               + system_time / 100
846095cf55dSPaolo Bonzini  *
847095cf55dSPaolo Bonzini  * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out:
848095cf55dSPaolo Bonzini  *    offset = system_time / 100 - tsc_timestamp * scale / 2^64
849095cf55dSPaolo Bonzini  *
850095cf55dSPaolo Bonzini  * These two equivalencies are implemented in this function.
851095cf55dSPaolo Bonzini  */
852095cf55dSPaolo Bonzini static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock,
853095cf55dSPaolo Bonzini 					HV_REFERENCE_TSC_PAGE *tsc_ref)
854095cf55dSPaolo Bonzini {
855095cf55dSPaolo Bonzini 	u64 max_mul;
856095cf55dSPaolo Bonzini 
857095cf55dSPaolo Bonzini 	if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT))
858095cf55dSPaolo Bonzini 		return false;
859095cf55dSPaolo Bonzini 
860095cf55dSPaolo Bonzini 	/*
861095cf55dSPaolo Bonzini 	 * check if scale would overflow, if so we use the time ref counter
862095cf55dSPaolo Bonzini 	 *    tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64
863095cf55dSPaolo Bonzini 	 *    tsc_to_system_mul / 100 >= 2^(32-tsc_shift)
864095cf55dSPaolo Bonzini 	 *    tsc_to_system_mul >= 100 * 2^(32-tsc_shift)
865095cf55dSPaolo Bonzini 	 */
866095cf55dSPaolo Bonzini 	max_mul = 100ull << (32 - hv_clock->tsc_shift);
867095cf55dSPaolo Bonzini 	if (hv_clock->tsc_to_system_mul >= max_mul)
868095cf55dSPaolo Bonzini 		return false;
869095cf55dSPaolo Bonzini 
870095cf55dSPaolo Bonzini 	/*
871095cf55dSPaolo Bonzini 	 * Otherwise compute the scale and offset according to the formulas
872095cf55dSPaolo Bonzini 	 * derived above.
873095cf55dSPaolo Bonzini 	 */
874095cf55dSPaolo Bonzini 	tsc_ref->tsc_scale =
875095cf55dSPaolo Bonzini 		mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift),
876095cf55dSPaolo Bonzini 				hv_clock->tsc_to_system_mul,
877095cf55dSPaolo Bonzini 				100);
878095cf55dSPaolo Bonzini 
879095cf55dSPaolo Bonzini 	tsc_ref->tsc_offset = hv_clock->system_time;
880095cf55dSPaolo Bonzini 	do_div(tsc_ref->tsc_offset, 100);
881095cf55dSPaolo Bonzini 	tsc_ref->tsc_offset -=
882095cf55dSPaolo Bonzini 		mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64);
883095cf55dSPaolo Bonzini 	return true;
884095cf55dSPaolo Bonzini }
885095cf55dSPaolo Bonzini 
886095cf55dSPaolo Bonzini void kvm_hv_setup_tsc_page(struct kvm *kvm,
887095cf55dSPaolo Bonzini 			   struct pvclock_vcpu_time_info *hv_clock)
888095cf55dSPaolo Bonzini {
889095cf55dSPaolo Bonzini 	struct kvm_hv *hv = &kvm->arch.hyperv;
890095cf55dSPaolo Bonzini 	u32 tsc_seq;
891095cf55dSPaolo Bonzini 	u64 gfn;
892095cf55dSPaolo Bonzini 
893095cf55dSPaolo Bonzini 	BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence));
894095cf55dSPaolo Bonzini 	BUILD_BUG_ON(offsetof(HV_REFERENCE_TSC_PAGE, tsc_sequence) != 0);
895095cf55dSPaolo Bonzini 
896095cf55dSPaolo Bonzini 	if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
897095cf55dSPaolo Bonzini 		return;
898095cf55dSPaolo Bonzini 
8993f5ad8beSPaolo Bonzini 	mutex_lock(&kvm->arch.hyperv.hv_lock);
9003f5ad8beSPaolo Bonzini 	if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
9013f5ad8beSPaolo Bonzini 		goto out_unlock;
9023f5ad8beSPaolo Bonzini 
903095cf55dSPaolo Bonzini 	gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
904095cf55dSPaolo Bonzini 	/*
905095cf55dSPaolo Bonzini 	 * Because the TSC parameters only vary when there is a
906095cf55dSPaolo Bonzini 	 * change in the master clock, do not bother with caching.
907095cf55dSPaolo Bonzini 	 */
908095cf55dSPaolo Bonzini 	if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn),
909095cf55dSPaolo Bonzini 				    &tsc_seq, sizeof(tsc_seq))))
9103f5ad8beSPaolo Bonzini 		goto out_unlock;
911095cf55dSPaolo Bonzini 
912095cf55dSPaolo Bonzini 	/*
913095cf55dSPaolo Bonzini 	 * While we're computing and writing the parameters, force the
914095cf55dSPaolo Bonzini 	 * guest to use the time reference count MSR.
915095cf55dSPaolo Bonzini 	 */
916095cf55dSPaolo Bonzini 	hv->tsc_ref.tsc_sequence = 0;
917095cf55dSPaolo Bonzini 	if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
918095cf55dSPaolo Bonzini 			    &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
9193f5ad8beSPaolo Bonzini 		goto out_unlock;
920095cf55dSPaolo Bonzini 
921095cf55dSPaolo Bonzini 	if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref))
9223f5ad8beSPaolo Bonzini 		goto out_unlock;
923095cf55dSPaolo Bonzini 
924095cf55dSPaolo Bonzini 	/* Ensure sequence is zero before writing the rest of the struct.  */
925095cf55dSPaolo Bonzini 	smp_wmb();
926095cf55dSPaolo Bonzini 	if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
9273f5ad8beSPaolo Bonzini 		goto out_unlock;
928095cf55dSPaolo Bonzini 
929095cf55dSPaolo Bonzini 	/*
930095cf55dSPaolo Bonzini 	 * Now switch to the TSC page mechanism by writing the sequence.
931095cf55dSPaolo Bonzini 	 */
932095cf55dSPaolo Bonzini 	tsc_seq++;
933095cf55dSPaolo Bonzini 	if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0)
934095cf55dSPaolo Bonzini 		tsc_seq = 1;
935095cf55dSPaolo Bonzini 
936095cf55dSPaolo Bonzini 	/* Write the struct entirely before the non-zero sequence.  */
937095cf55dSPaolo Bonzini 	smp_wmb();
938095cf55dSPaolo Bonzini 
939095cf55dSPaolo Bonzini 	hv->tsc_ref.tsc_sequence = tsc_seq;
940095cf55dSPaolo Bonzini 	kvm_write_guest(kvm, gfn_to_gpa(gfn),
941095cf55dSPaolo Bonzini 			&hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence));
9423f5ad8beSPaolo Bonzini out_unlock:
9433f5ad8beSPaolo Bonzini 	mutex_unlock(&kvm->arch.hyperv.hv_lock);
944095cf55dSPaolo Bonzini }
945095cf55dSPaolo Bonzini 
946e7d9513bSAndrey Smetanin static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
947e7d9513bSAndrey Smetanin 			     bool host)
948e83d5887SAndrey Smetanin {
949e83d5887SAndrey Smetanin 	struct kvm *kvm = vcpu->kvm;
950e83d5887SAndrey Smetanin 	struct kvm_hv *hv = &kvm->arch.hyperv;
951e83d5887SAndrey Smetanin 
952e83d5887SAndrey Smetanin 	switch (msr) {
953e83d5887SAndrey Smetanin 	case HV_X64_MSR_GUEST_OS_ID:
954e83d5887SAndrey Smetanin 		hv->hv_guest_os_id = data;
955e83d5887SAndrey Smetanin 		/* setting guest os id to zero disables hypercall page */
956e83d5887SAndrey Smetanin 		if (!hv->hv_guest_os_id)
957e83d5887SAndrey Smetanin 			hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
958e83d5887SAndrey Smetanin 		break;
959e83d5887SAndrey Smetanin 	case HV_X64_MSR_HYPERCALL: {
960e83d5887SAndrey Smetanin 		u64 gfn;
961e83d5887SAndrey Smetanin 		unsigned long addr;
962e83d5887SAndrey Smetanin 		u8 instructions[4];
963e83d5887SAndrey Smetanin 
964e83d5887SAndrey Smetanin 		/* if guest os id is not set hypercall should remain disabled */
965e83d5887SAndrey Smetanin 		if (!hv->hv_guest_os_id)
966e83d5887SAndrey Smetanin 			break;
967e83d5887SAndrey Smetanin 		if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
968e83d5887SAndrey Smetanin 			hv->hv_hypercall = data;
969e83d5887SAndrey Smetanin 			break;
970e83d5887SAndrey Smetanin 		}
971e83d5887SAndrey Smetanin 		gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
972e83d5887SAndrey Smetanin 		addr = gfn_to_hva(kvm, gfn);
973e83d5887SAndrey Smetanin 		if (kvm_is_error_hva(addr))
974e83d5887SAndrey Smetanin 			return 1;
975e83d5887SAndrey Smetanin 		kvm_x86_ops->patch_hypercall(vcpu, instructions);
976e83d5887SAndrey Smetanin 		((unsigned char *)instructions)[3] = 0xc3; /* ret */
977e83d5887SAndrey Smetanin 		if (__copy_to_user((void __user *)addr, instructions, 4))
978e83d5887SAndrey Smetanin 			return 1;
979e83d5887SAndrey Smetanin 		hv->hv_hypercall = data;
980e83d5887SAndrey Smetanin 		mark_page_dirty(kvm, gfn);
981e83d5887SAndrey Smetanin 		break;
982e83d5887SAndrey Smetanin 	}
983095cf55dSPaolo Bonzini 	case HV_X64_MSR_REFERENCE_TSC:
984e83d5887SAndrey Smetanin 		hv->hv_tsc_page = data;
985095cf55dSPaolo Bonzini 		if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)
986095cf55dSPaolo Bonzini 			kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
987e83d5887SAndrey Smetanin 		break;
988e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
989e7d9513bSAndrey Smetanin 		return kvm_hv_msr_set_crash_data(vcpu,
990e7d9513bSAndrey Smetanin 						 msr - HV_X64_MSR_CRASH_P0,
991e7d9513bSAndrey Smetanin 						 data);
992e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_CTL:
993e7d9513bSAndrey Smetanin 		return kvm_hv_msr_set_crash_ctl(vcpu, data, host);
994e516cebbSAndrey Smetanin 	case HV_X64_MSR_RESET:
995e516cebbSAndrey Smetanin 		if (data == 1) {
996e516cebbSAndrey Smetanin 			vcpu_debug(vcpu, "hyper-v reset requested\n");
997e516cebbSAndrey Smetanin 			kvm_make_request(KVM_REQ_HV_RESET, vcpu);
998e516cebbSAndrey Smetanin 		}
999e516cebbSAndrey Smetanin 		break;
1000a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1001a2e164e7SVitaly Kuznetsov 		hv->hv_reenlightenment_control = data;
1002a2e164e7SVitaly Kuznetsov 		break;
1003a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_TSC_EMULATION_CONTROL:
1004a2e164e7SVitaly Kuznetsov 		hv->hv_tsc_emulation_control = data;
1005a2e164e7SVitaly Kuznetsov 		break;
1006a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_TSC_EMULATION_STATUS:
1007a2e164e7SVitaly Kuznetsov 		hv->hv_tsc_emulation_status = data;
1008a2e164e7SVitaly Kuznetsov 		break;
1009e83d5887SAndrey Smetanin 	default:
1010e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
1011e83d5887SAndrey Smetanin 			    msr, data);
1012e83d5887SAndrey Smetanin 		return 1;
1013e83d5887SAndrey Smetanin 	}
1014e83d5887SAndrey Smetanin 	return 0;
1015e83d5887SAndrey Smetanin }
1016e83d5887SAndrey Smetanin 
10179eec50b8SAndrey Smetanin /* Calculate cpu time spent by current task in 100ns units */
10189eec50b8SAndrey Smetanin static u64 current_task_runtime_100ns(void)
10199eec50b8SAndrey Smetanin {
10205613fda9SFrederic Weisbecker 	u64 utime, stime;
10219eec50b8SAndrey Smetanin 
10229eec50b8SAndrey Smetanin 	task_cputime_adjusted(current, &utime, &stime);
10235613fda9SFrederic Weisbecker 
10245613fda9SFrederic Weisbecker 	return div_u64(utime + stime, 100);
10259eec50b8SAndrey Smetanin }
10269eec50b8SAndrey Smetanin 
10279eec50b8SAndrey Smetanin static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
1028e83d5887SAndrey Smetanin {
1029e83d5887SAndrey Smetanin 	struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
1030e83d5887SAndrey Smetanin 
1031e83d5887SAndrey Smetanin 	switch (msr) {
1032d3457c87SRoman Kagan 	case HV_X64_MSR_VP_INDEX:
1033d3457c87SRoman Kagan 		if (!host)
1034d3457c87SRoman Kagan 			return 1;
1035d3457c87SRoman Kagan 		hv->vp_index = (u32)data;
1036d3457c87SRoman Kagan 		break;
1037e83d5887SAndrey Smetanin 	case HV_X64_MSR_APIC_ASSIST_PAGE: {
1038e83d5887SAndrey Smetanin 		u64 gfn;
1039e83d5887SAndrey Smetanin 		unsigned long addr;
1040e83d5887SAndrey Smetanin 
1041e83d5887SAndrey Smetanin 		if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
1042e83d5887SAndrey Smetanin 			hv->hv_vapic = data;
1043e83d5887SAndrey Smetanin 			if (kvm_lapic_enable_pv_eoi(vcpu, 0))
1044e83d5887SAndrey Smetanin 				return 1;
1045e83d5887SAndrey Smetanin 			break;
1046e83d5887SAndrey Smetanin 		}
1047e83d5887SAndrey Smetanin 		gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT;
1048e83d5887SAndrey Smetanin 		addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
1049e83d5887SAndrey Smetanin 		if (kvm_is_error_hva(addr))
1050e83d5887SAndrey Smetanin 			return 1;
1051e83d5887SAndrey Smetanin 		if (__clear_user((void __user *)addr, PAGE_SIZE))
1052e83d5887SAndrey Smetanin 			return 1;
1053e83d5887SAndrey Smetanin 		hv->hv_vapic = data;
1054e83d5887SAndrey Smetanin 		kvm_vcpu_mark_page_dirty(vcpu, gfn);
1055e83d5887SAndrey Smetanin 		if (kvm_lapic_enable_pv_eoi(vcpu,
1056e83d5887SAndrey Smetanin 					    gfn_to_gpa(gfn) | KVM_MSR_ENABLED))
1057e83d5887SAndrey Smetanin 			return 1;
1058e83d5887SAndrey Smetanin 		break;
1059e83d5887SAndrey Smetanin 	}
1060e83d5887SAndrey Smetanin 	case HV_X64_MSR_EOI:
1061e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1062e83d5887SAndrey Smetanin 	case HV_X64_MSR_ICR:
1063e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1064e83d5887SAndrey Smetanin 	case HV_X64_MSR_TPR:
1065e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
10669eec50b8SAndrey Smetanin 	case HV_X64_MSR_VP_RUNTIME:
10679eec50b8SAndrey Smetanin 		if (!host)
10689eec50b8SAndrey Smetanin 			return 1;
10699eec50b8SAndrey Smetanin 		hv->runtime_offset = data - current_task_runtime_100ns();
10709eec50b8SAndrey Smetanin 		break;
10715c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
10725c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
10735c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
10745c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
10755c919412SAndrey Smetanin 	case HV_X64_MSR_EOM:
10765c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
10775c919412SAndrey Smetanin 		return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host);
10781f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER0_CONFIG:
10791f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER1_CONFIG:
10801f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER2_CONFIG:
10811f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER3_CONFIG: {
10821f4b34f8SAndrey Smetanin 		int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
10831f4b34f8SAndrey Smetanin 
10841f4b34f8SAndrey Smetanin 		return stimer_set_config(vcpu_to_stimer(vcpu, timer_index),
10851f4b34f8SAndrey Smetanin 					 data, host);
10861f4b34f8SAndrey Smetanin 	}
10871f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER0_COUNT:
10881f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER1_COUNT:
10891f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER2_COUNT:
10901f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER3_COUNT: {
10911f4b34f8SAndrey Smetanin 		int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
10921f4b34f8SAndrey Smetanin 
10931f4b34f8SAndrey Smetanin 		return stimer_set_count(vcpu_to_stimer(vcpu, timer_index),
10941f4b34f8SAndrey Smetanin 					data, host);
10951f4b34f8SAndrey Smetanin 	}
1096e83d5887SAndrey Smetanin 	default:
1097e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
1098e83d5887SAndrey Smetanin 			    msr, data);
1099e83d5887SAndrey Smetanin 		return 1;
1100e83d5887SAndrey Smetanin 	}
1101e83d5887SAndrey Smetanin 
1102e83d5887SAndrey Smetanin 	return 0;
1103e83d5887SAndrey Smetanin }
1104e83d5887SAndrey Smetanin 
1105e83d5887SAndrey Smetanin static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1106e83d5887SAndrey Smetanin {
1107e83d5887SAndrey Smetanin 	u64 data = 0;
1108e83d5887SAndrey Smetanin 	struct kvm *kvm = vcpu->kvm;
1109e83d5887SAndrey Smetanin 	struct kvm_hv *hv = &kvm->arch.hyperv;
1110e83d5887SAndrey Smetanin 
1111e83d5887SAndrey Smetanin 	switch (msr) {
1112e83d5887SAndrey Smetanin 	case HV_X64_MSR_GUEST_OS_ID:
1113e83d5887SAndrey Smetanin 		data = hv->hv_guest_os_id;
1114e83d5887SAndrey Smetanin 		break;
1115e83d5887SAndrey Smetanin 	case HV_X64_MSR_HYPERCALL:
1116e83d5887SAndrey Smetanin 		data = hv->hv_hypercall;
1117e83d5887SAndrey Smetanin 		break;
111893bf4172SAndrey Smetanin 	case HV_X64_MSR_TIME_REF_COUNT:
111993bf4172SAndrey Smetanin 		data = get_time_ref_counter(kvm);
1120e83d5887SAndrey Smetanin 		break;
1121e83d5887SAndrey Smetanin 	case HV_X64_MSR_REFERENCE_TSC:
1122e83d5887SAndrey Smetanin 		data = hv->hv_tsc_page;
1123e83d5887SAndrey Smetanin 		break;
1124e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1125e7d9513bSAndrey Smetanin 		return kvm_hv_msr_get_crash_data(vcpu,
1126e7d9513bSAndrey Smetanin 						 msr - HV_X64_MSR_CRASH_P0,
1127e7d9513bSAndrey Smetanin 						 pdata);
1128e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_CTL:
1129e7d9513bSAndrey Smetanin 		return kvm_hv_msr_get_crash_ctl(vcpu, pdata);
1130e516cebbSAndrey Smetanin 	case HV_X64_MSR_RESET:
1131e516cebbSAndrey Smetanin 		data = 0;
1132e516cebbSAndrey Smetanin 		break;
1133a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1134a2e164e7SVitaly Kuznetsov 		data = hv->hv_reenlightenment_control;
1135a2e164e7SVitaly Kuznetsov 		break;
1136a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_TSC_EMULATION_CONTROL:
1137a2e164e7SVitaly Kuznetsov 		data = hv->hv_tsc_emulation_control;
1138a2e164e7SVitaly Kuznetsov 		break;
1139a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_TSC_EMULATION_STATUS:
1140a2e164e7SVitaly Kuznetsov 		data = hv->hv_tsc_emulation_status;
1141a2e164e7SVitaly Kuznetsov 		break;
1142e83d5887SAndrey Smetanin 	default:
1143e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1144e83d5887SAndrey Smetanin 		return 1;
1145e83d5887SAndrey Smetanin 	}
1146e83d5887SAndrey Smetanin 
1147e83d5887SAndrey Smetanin 	*pdata = data;
1148e83d5887SAndrey Smetanin 	return 0;
1149e83d5887SAndrey Smetanin }
1150e83d5887SAndrey Smetanin 
1151e83d5887SAndrey Smetanin static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1152e83d5887SAndrey Smetanin {
1153e83d5887SAndrey Smetanin 	u64 data = 0;
1154e83d5887SAndrey Smetanin 	struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
1155e83d5887SAndrey Smetanin 
1156e83d5887SAndrey Smetanin 	switch (msr) {
1157d3457c87SRoman Kagan 	case HV_X64_MSR_VP_INDEX:
1158d3457c87SRoman Kagan 		data = hv->vp_index;
1159e83d5887SAndrey Smetanin 		break;
1160e83d5887SAndrey Smetanin 	case HV_X64_MSR_EOI:
1161e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1162e83d5887SAndrey Smetanin 	case HV_X64_MSR_ICR:
1163e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1164e83d5887SAndrey Smetanin 	case HV_X64_MSR_TPR:
1165e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
1166e83d5887SAndrey Smetanin 	case HV_X64_MSR_APIC_ASSIST_PAGE:
1167e83d5887SAndrey Smetanin 		data = hv->hv_vapic;
1168e83d5887SAndrey Smetanin 		break;
11699eec50b8SAndrey Smetanin 	case HV_X64_MSR_VP_RUNTIME:
11709eec50b8SAndrey Smetanin 		data = current_task_runtime_100ns() + hv->runtime_offset;
11719eec50b8SAndrey Smetanin 		break;
11725c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
11735c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
11745c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
11755c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
11765c919412SAndrey Smetanin 	case HV_X64_MSR_EOM:
11775c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
11785c919412SAndrey Smetanin 		return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata);
11791f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER0_CONFIG:
11801f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER1_CONFIG:
11811f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER2_CONFIG:
11821f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER3_CONFIG: {
11831f4b34f8SAndrey Smetanin 		int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
11841f4b34f8SAndrey Smetanin 
11851f4b34f8SAndrey Smetanin 		return stimer_get_config(vcpu_to_stimer(vcpu, timer_index),
11861f4b34f8SAndrey Smetanin 					 pdata);
11871f4b34f8SAndrey Smetanin 	}
11881f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER0_COUNT:
11891f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER1_COUNT:
11901f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER2_COUNT:
11911f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER3_COUNT: {
11921f4b34f8SAndrey Smetanin 		int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
11931f4b34f8SAndrey Smetanin 
11941f4b34f8SAndrey Smetanin 		return stimer_get_count(vcpu_to_stimer(vcpu, timer_index),
11951f4b34f8SAndrey Smetanin 					pdata);
11961f4b34f8SAndrey Smetanin 	}
119772c139baSLadi Prosek 	case HV_X64_MSR_TSC_FREQUENCY:
119872c139baSLadi Prosek 		data = (u64)vcpu->arch.virtual_tsc_khz * 1000;
119972c139baSLadi Prosek 		break;
120072c139baSLadi Prosek 	case HV_X64_MSR_APIC_FREQUENCY:
120172c139baSLadi Prosek 		data = APIC_BUS_FREQUENCY;
120272c139baSLadi Prosek 		break;
1203e83d5887SAndrey Smetanin 	default:
1204e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1205e83d5887SAndrey Smetanin 		return 1;
1206e83d5887SAndrey Smetanin 	}
1207e83d5887SAndrey Smetanin 	*pdata = data;
1208e83d5887SAndrey Smetanin 	return 0;
1209e83d5887SAndrey Smetanin }
1210e83d5887SAndrey Smetanin 
1211e7d9513bSAndrey Smetanin int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
1212e83d5887SAndrey Smetanin {
1213e83d5887SAndrey Smetanin 	if (kvm_hv_msr_partition_wide(msr)) {
1214e83d5887SAndrey Smetanin 		int r;
1215e83d5887SAndrey Smetanin 
12163f5ad8beSPaolo Bonzini 		mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
1217e7d9513bSAndrey Smetanin 		r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
12183f5ad8beSPaolo Bonzini 		mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
1219e83d5887SAndrey Smetanin 		return r;
1220e83d5887SAndrey Smetanin 	} else
12219eec50b8SAndrey Smetanin 		return kvm_hv_set_msr(vcpu, msr, data, host);
1222e83d5887SAndrey Smetanin }
1223e83d5887SAndrey Smetanin 
1224e83d5887SAndrey Smetanin int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1225e83d5887SAndrey Smetanin {
1226e83d5887SAndrey Smetanin 	if (kvm_hv_msr_partition_wide(msr)) {
1227e83d5887SAndrey Smetanin 		int r;
1228e83d5887SAndrey Smetanin 
12293f5ad8beSPaolo Bonzini 		mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
1230e83d5887SAndrey Smetanin 		r = kvm_hv_get_msr_pw(vcpu, msr, pdata);
12313f5ad8beSPaolo Bonzini 		mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
1232e83d5887SAndrey Smetanin 		return r;
1233e83d5887SAndrey Smetanin 	} else
1234e83d5887SAndrey Smetanin 		return kvm_hv_get_msr(vcpu, msr, pdata);
1235e83d5887SAndrey Smetanin }
1236e83d5887SAndrey Smetanin 
1237e83d5887SAndrey Smetanin bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1238e83d5887SAndrey Smetanin {
12393f5ad8beSPaolo Bonzini 	return READ_ONCE(kvm->arch.hyperv.hv_hypercall) & HV_X64_MSR_HYPERCALL_ENABLE;
1240e83d5887SAndrey Smetanin }
1241e83d5887SAndrey Smetanin 
124283326e43SAndrey Smetanin static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
124383326e43SAndrey Smetanin {
124483326e43SAndrey Smetanin 	bool longmode;
124583326e43SAndrey Smetanin 
124683326e43SAndrey Smetanin 	longmode = is_64_bit_mode(vcpu);
124783326e43SAndrey Smetanin 	if (longmode)
124883326e43SAndrey Smetanin 		kvm_register_write(vcpu, VCPU_REGS_RAX, result);
124983326e43SAndrey Smetanin 	else {
125083326e43SAndrey Smetanin 		kvm_register_write(vcpu, VCPU_REGS_RDX, result >> 32);
125183326e43SAndrey Smetanin 		kvm_register_write(vcpu, VCPU_REGS_RAX, result & 0xffffffff);
125283326e43SAndrey Smetanin 	}
125383326e43SAndrey Smetanin }
125483326e43SAndrey Smetanin 
125583326e43SAndrey Smetanin static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
125683326e43SAndrey Smetanin {
125783326e43SAndrey Smetanin 	struct kvm_run *run = vcpu->run;
125883326e43SAndrey Smetanin 
125983326e43SAndrey Smetanin 	kvm_hv_hypercall_set_result(vcpu, run->hyperv.u.hcall.result);
126083326e43SAndrey Smetanin 	return 1;
126183326e43SAndrey Smetanin }
126283326e43SAndrey Smetanin 
1263faeb7833SRoman Kagan static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, bool fast, u64 param)
1264faeb7833SRoman Kagan {
1265faeb7833SRoman Kagan 	struct eventfd_ctx *eventfd;
1266faeb7833SRoman Kagan 
1267faeb7833SRoman Kagan 	if (unlikely(!fast)) {
1268faeb7833SRoman Kagan 		int ret;
1269faeb7833SRoman Kagan 		gpa_t gpa = param;
1270faeb7833SRoman Kagan 
1271faeb7833SRoman Kagan 		if ((gpa & (__alignof__(param) - 1)) ||
1272faeb7833SRoman Kagan 		    offset_in_page(gpa) + sizeof(param) > PAGE_SIZE)
1273faeb7833SRoman Kagan 			return HV_STATUS_INVALID_ALIGNMENT;
1274faeb7833SRoman Kagan 
1275faeb7833SRoman Kagan 		ret = kvm_vcpu_read_guest(vcpu, gpa, &param, sizeof(param));
1276faeb7833SRoman Kagan 		if (ret < 0)
1277faeb7833SRoman Kagan 			return HV_STATUS_INVALID_ALIGNMENT;
1278faeb7833SRoman Kagan 	}
1279faeb7833SRoman Kagan 
1280faeb7833SRoman Kagan 	/*
1281faeb7833SRoman Kagan 	 * Per spec, bits 32-47 contain the extra "flag number".  However, we
1282faeb7833SRoman Kagan 	 * have no use for it, and in all known usecases it is zero, so just
1283faeb7833SRoman Kagan 	 * report lookup failure if it isn't.
1284faeb7833SRoman Kagan 	 */
1285faeb7833SRoman Kagan 	if (param & 0xffff00000000ULL)
1286faeb7833SRoman Kagan 		return HV_STATUS_INVALID_PORT_ID;
1287faeb7833SRoman Kagan 	/* remaining bits are reserved-zero */
1288faeb7833SRoman Kagan 	if (param & ~KVM_HYPERV_CONN_ID_MASK)
1289faeb7833SRoman Kagan 		return HV_STATUS_INVALID_HYPERCALL_INPUT;
1290faeb7833SRoman Kagan 
1291faeb7833SRoman Kagan 	/* conn_to_evt is protected by vcpu->kvm->srcu */
1292faeb7833SRoman Kagan 	eventfd = idr_find(&vcpu->kvm->arch.hyperv.conn_to_evt, param);
1293faeb7833SRoman Kagan 	if (!eventfd)
1294faeb7833SRoman Kagan 		return HV_STATUS_INVALID_PORT_ID;
1295faeb7833SRoman Kagan 
1296faeb7833SRoman Kagan 	eventfd_signal(eventfd, 1);
1297faeb7833SRoman Kagan 	return HV_STATUS_SUCCESS;
1298faeb7833SRoman Kagan }
1299faeb7833SRoman Kagan 
1300e83d5887SAndrey Smetanin int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
1301e83d5887SAndrey Smetanin {
1302e83d5887SAndrey Smetanin 	u64 param, ingpa, outgpa, ret;
1303e83d5887SAndrey Smetanin 	uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
1304e83d5887SAndrey Smetanin 	bool fast, longmode;
1305e83d5887SAndrey Smetanin 
1306e83d5887SAndrey Smetanin 	/*
1307e83d5887SAndrey Smetanin 	 * hypercall generates UD from non zero cpl and real mode
1308e83d5887SAndrey Smetanin 	 * per HYPER-V spec
1309e83d5887SAndrey Smetanin 	 */
1310e83d5887SAndrey Smetanin 	if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
1311e83d5887SAndrey Smetanin 		kvm_queue_exception(vcpu, UD_VECTOR);
13120d9c055eSAndrey Smetanin 		return 1;
1313e83d5887SAndrey Smetanin 	}
1314e83d5887SAndrey Smetanin 
1315e83d5887SAndrey Smetanin 	longmode = is_64_bit_mode(vcpu);
1316e83d5887SAndrey Smetanin 
1317e83d5887SAndrey Smetanin 	if (!longmode) {
1318e83d5887SAndrey Smetanin 		param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
1319e83d5887SAndrey Smetanin 			(kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
1320e83d5887SAndrey Smetanin 		ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
1321e83d5887SAndrey Smetanin 			(kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
1322e83d5887SAndrey Smetanin 		outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
1323e83d5887SAndrey Smetanin 			(kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
1324e83d5887SAndrey Smetanin 	}
1325e83d5887SAndrey Smetanin #ifdef CONFIG_X86_64
1326e83d5887SAndrey Smetanin 	else {
1327e83d5887SAndrey Smetanin 		param = kvm_register_read(vcpu, VCPU_REGS_RCX);
1328e83d5887SAndrey Smetanin 		ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
1329e83d5887SAndrey Smetanin 		outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
1330e83d5887SAndrey Smetanin 	}
1331e83d5887SAndrey Smetanin #endif
1332e83d5887SAndrey Smetanin 
1333e83d5887SAndrey Smetanin 	code = param & 0xffff;
1334e83d5887SAndrey Smetanin 	fast = (param >> 16) & 0x1;
1335e83d5887SAndrey Smetanin 	rep_cnt = (param >> 32) & 0xfff;
1336e83d5887SAndrey Smetanin 	rep_idx = (param >> 48) & 0xfff;
1337e83d5887SAndrey Smetanin 
1338e83d5887SAndrey Smetanin 	trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
1339e83d5887SAndrey Smetanin 
1340b2fdc257SAndrey Smetanin 	/* Hypercall continuation is not supported yet */
1341b2fdc257SAndrey Smetanin 	if (rep_cnt || rep_idx) {
1342b2fdc257SAndrey Smetanin 		res = HV_STATUS_INVALID_HYPERCALL_CODE;
1343b2fdc257SAndrey Smetanin 		goto set_result;
1344b2fdc257SAndrey Smetanin 	}
1345b2fdc257SAndrey Smetanin 
1346e83d5887SAndrey Smetanin 	switch (code) {
13478ed6d767SAndrey Smetanin 	case HVCALL_NOTIFY_LONG_SPIN_WAIT:
1348de63ad4cSLongpeng(Mike) 		kvm_vcpu_on_spin(vcpu, true);
1349e83d5887SAndrey Smetanin 		break;
135083326e43SAndrey Smetanin 	case HVCALL_SIGNAL_EVENT:
1351faeb7833SRoman Kagan 		res = kvm_hvcall_signal_event(vcpu, fast, ingpa);
1352faeb7833SRoman Kagan 		if (res != HV_STATUS_INVALID_PORT_ID)
1353faeb7833SRoman Kagan 			break;
1354faeb7833SRoman Kagan 		/* maybe userspace knows this conn_id: fall through */
1355faeb7833SRoman Kagan 	case HVCALL_POST_MESSAGE:
1356a2b5c3c0SPaolo Bonzini 		/* don't bother userspace if it has no way to handle it */
1357a2b5c3c0SPaolo Bonzini 		if (!vcpu_to_synic(vcpu)->active) {
1358a2b5c3c0SPaolo Bonzini 			res = HV_STATUS_INVALID_HYPERCALL_CODE;
1359a2b5c3c0SPaolo Bonzini 			break;
1360a2b5c3c0SPaolo Bonzini 		}
136183326e43SAndrey Smetanin 		vcpu->run->exit_reason = KVM_EXIT_HYPERV;
136283326e43SAndrey Smetanin 		vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
136383326e43SAndrey Smetanin 		vcpu->run->hyperv.u.hcall.input = param;
136483326e43SAndrey Smetanin 		vcpu->run->hyperv.u.hcall.params[0] = ingpa;
136583326e43SAndrey Smetanin 		vcpu->run->hyperv.u.hcall.params[1] = outgpa;
136683326e43SAndrey Smetanin 		vcpu->arch.complete_userspace_io =
136783326e43SAndrey Smetanin 				kvm_hv_hypercall_complete_userspace;
136883326e43SAndrey Smetanin 		return 0;
1369e83d5887SAndrey Smetanin 	default:
1370e83d5887SAndrey Smetanin 		res = HV_STATUS_INVALID_HYPERCALL_CODE;
1371e83d5887SAndrey Smetanin 		break;
1372e83d5887SAndrey Smetanin 	}
1373e83d5887SAndrey Smetanin 
1374b2fdc257SAndrey Smetanin set_result:
1375e83d5887SAndrey Smetanin 	ret = res | (((u64)rep_done & 0xfff) << 32);
137683326e43SAndrey Smetanin 	kvm_hv_hypercall_set_result(vcpu, ret);
1377e83d5887SAndrey Smetanin 	return 1;
1378e83d5887SAndrey Smetanin }
1379cbc0236aSRoman Kagan 
1380cbc0236aSRoman Kagan void kvm_hv_init_vm(struct kvm *kvm)
1381cbc0236aSRoman Kagan {
1382cbc0236aSRoman Kagan 	mutex_init(&kvm->arch.hyperv.hv_lock);
1383faeb7833SRoman Kagan 	idr_init(&kvm->arch.hyperv.conn_to_evt);
1384cbc0236aSRoman Kagan }
1385cbc0236aSRoman Kagan 
1386cbc0236aSRoman Kagan void kvm_hv_destroy_vm(struct kvm *kvm)
1387cbc0236aSRoman Kagan {
1388faeb7833SRoman Kagan 	struct eventfd_ctx *eventfd;
1389faeb7833SRoman Kagan 	int i;
1390faeb7833SRoman Kagan 
1391faeb7833SRoman Kagan 	idr_for_each_entry(&kvm->arch.hyperv.conn_to_evt, eventfd, i)
1392faeb7833SRoman Kagan 		eventfd_ctx_put(eventfd);
1393faeb7833SRoman Kagan 	idr_destroy(&kvm->arch.hyperv.conn_to_evt);
1394faeb7833SRoman Kagan }
1395faeb7833SRoman Kagan 
1396faeb7833SRoman Kagan static int kvm_hv_eventfd_assign(struct kvm *kvm, u32 conn_id, int fd)
1397faeb7833SRoman Kagan {
1398faeb7833SRoman Kagan 	struct kvm_hv *hv = &kvm->arch.hyperv;
1399faeb7833SRoman Kagan 	struct eventfd_ctx *eventfd;
1400faeb7833SRoman Kagan 	int ret;
1401faeb7833SRoman Kagan 
1402faeb7833SRoman Kagan 	eventfd = eventfd_ctx_fdget(fd);
1403faeb7833SRoman Kagan 	if (IS_ERR(eventfd))
1404faeb7833SRoman Kagan 		return PTR_ERR(eventfd);
1405faeb7833SRoman Kagan 
1406faeb7833SRoman Kagan 	mutex_lock(&hv->hv_lock);
1407faeb7833SRoman Kagan 	ret = idr_alloc(&hv->conn_to_evt, eventfd, conn_id, conn_id + 1,
1408faeb7833SRoman Kagan 			GFP_KERNEL);
1409faeb7833SRoman Kagan 	mutex_unlock(&hv->hv_lock);
1410faeb7833SRoman Kagan 
1411faeb7833SRoman Kagan 	if (ret >= 0)
1412faeb7833SRoman Kagan 		return 0;
1413faeb7833SRoman Kagan 
1414faeb7833SRoman Kagan 	if (ret == -ENOSPC)
1415faeb7833SRoman Kagan 		ret = -EEXIST;
1416faeb7833SRoman Kagan 	eventfd_ctx_put(eventfd);
1417faeb7833SRoman Kagan 	return ret;
1418faeb7833SRoman Kagan }
1419faeb7833SRoman Kagan 
1420faeb7833SRoman Kagan static int kvm_hv_eventfd_deassign(struct kvm *kvm, u32 conn_id)
1421faeb7833SRoman Kagan {
1422faeb7833SRoman Kagan 	struct kvm_hv *hv = &kvm->arch.hyperv;
1423faeb7833SRoman Kagan 	struct eventfd_ctx *eventfd;
1424faeb7833SRoman Kagan 
1425faeb7833SRoman Kagan 	mutex_lock(&hv->hv_lock);
1426faeb7833SRoman Kagan 	eventfd = idr_remove(&hv->conn_to_evt, conn_id);
1427faeb7833SRoman Kagan 	mutex_unlock(&hv->hv_lock);
1428faeb7833SRoman Kagan 
1429faeb7833SRoman Kagan 	if (!eventfd)
1430faeb7833SRoman Kagan 		return -ENOENT;
1431faeb7833SRoman Kagan 
1432faeb7833SRoman Kagan 	synchronize_srcu(&kvm->srcu);
1433faeb7833SRoman Kagan 	eventfd_ctx_put(eventfd);
1434faeb7833SRoman Kagan 	return 0;
1435faeb7833SRoman Kagan }
1436faeb7833SRoman Kagan 
1437faeb7833SRoman Kagan int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args)
1438faeb7833SRoman Kagan {
1439faeb7833SRoman Kagan 	if ((args->flags & ~KVM_HYPERV_EVENTFD_DEASSIGN) ||
1440faeb7833SRoman Kagan 	    (args->conn_id & ~KVM_HYPERV_CONN_ID_MASK))
1441faeb7833SRoman Kagan 		return -EINVAL;
1442faeb7833SRoman Kagan 
1443faeb7833SRoman Kagan 	if (args->flags == KVM_HYPERV_EVENTFD_DEASSIGN)
1444faeb7833SRoman Kagan 		return kvm_hv_eventfd_deassign(kvm, args->conn_id);
1445faeb7833SRoman Kagan 	return kvm_hv_eventfd_assign(kvm, args->conn_id, args->fd);
1446cbc0236aSRoman Kagan }
1447