xref: /openbmc/linux/arch/x86/kvm/hyperv.c (revision 254272ce6505948ecc0b4bf5cd0aa61cdd815994)
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 
39f21dd494SVitaly Kuznetsov #define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, 64)
40f21dd494SVitaly Kuznetsov 
418644f771SVitaly Kuznetsov static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
428644f771SVitaly Kuznetsov 				bool vcpu_kick);
438644f771SVitaly Kuznetsov 
445c919412SAndrey Smetanin static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
455c919412SAndrey Smetanin {
465c919412SAndrey Smetanin 	return atomic64_read(&synic->sint[sint]);
475c919412SAndrey Smetanin }
485c919412SAndrey Smetanin 
495c919412SAndrey Smetanin static inline int synic_get_sint_vector(u64 sint_value)
505c919412SAndrey Smetanin {
515c919412SAndrey Smetanin 	if (sint_value & HV_SYNIC_SINT_MASKED)
525c919412SAndrey Smetanin 		return -1;
535c919412SAndrey Smetanin 	return sint_value & HV_SYNIC_SINT_VECTOR_MASK;
545c919412SAndrey Smetanin }
555c919412SAndrey Smetanin 
565c919412SAndrey Smetanin static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic,
575c919412SAndrey Smetanin 				      int vector)
585c919412SAndrey Smetanin {
595c919412SAndrey Smetanin 	int i;
605c919412SAndrey Smetanin 
615c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
625c919412SAndrey Smetanin 		if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
635c919412SAndrey Smetanin 			return true;
645c919412SAndrey Smetanin 	}
655c919412SAndrey Smetanin 	return false;
665c919412SAndrey Smetanin }
675c919412SAndrey Smetanin 
685c919412SAndrey Smetanin static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic,
695c919412SAndrey Smetanin 				     int vector)
705c919412SAndrey Smetanin {
715c919412SAndrey Smetanin 	int i;
725c919412SAndrey Smetanin 	u64 sint_value;
735c919412SAndrey Smetanin 
745c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
755c919412SAndrey Smetanin 		sint_value = synic_read_sint(synic, i);
765c919412SAndrey Smetanin 		if (synic_get_sint_vector(sint_value) == vector &&
775c919412SAndrey Smetanin 		    sint_value & HV_SYNIC_SINT_AUTO_EOI)
785c919412SAndrey Smetanin 			return true;
795c919412SAndrey Smetanin 	}
805c919412SAndrey Smetanin 	return false;
815c919412SAndrey Smetanin }
825c919412SAndrey Smetanin 
8398f65ad4SVitaly Kuznetsov static void synic_update_vector(struct kvm_vcpu_hv_synic *synic,
8498f65ad4SVitaly Kuznetsov 				int vector)
855c919412SAndrey Smetanin {
8687a8d795SVitaly Kuznetsov 	if (vector < HV_SYNIC_FIRST_VALID_VECTOR)
8787a8d795SVitaly Kuznetsov 		return;
8887a8d795SVitaly Kuznetsov 
895c919412SAndrey Smetanin 	if (synic_has_vector_connected(synic, vector))
905c919412SAndrey Smetanin 		__set_bit(vector, synic->vec_bitmap);
915c919412SAndrey Smetanin 	else
925c919412SAndrey Smetanin 		__clear_bit(vector, synic->vec_bitmap);
935c919412SAndrey Smetanin 
945c919412SAndrey Smetanin 	if (synic_has_vector_auto_eoi(synic, vector))
955c919412SAndrey Smetanin 		__set_bit(vector, synic->auto_eoi_bitmap);
965c919412SAndrey Smetanin 	else
975c919412SAndrey Smetanin 		__clear_bit(vector, synic->auto_eoi_bitmap);
9898f65ad4SVitaly Kuznetsov }
9998f65ad4SVitaly Kuznetsov 
10098f65ad4SVitaly Kuznetsov static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint,
10198f65ad4SVitaly Kuznetsov 			  u64 data, bool host)
10298f65ad4SVitaly Kuznetsov {
10398f65ad4SVitaly Kuznetsov 	int vector, old_vector;
104915e6f78SVitaly Kuznetsov 	bool masked;
10598f65ad4SVitaly Kuznetsov 
10698f65ad4SVitaly Kuznetsov 	vector = data & HV_SYNIC_SINT_VECTOR_MASK;
107915e6f78SVitaly Kuznetsov 	masked = data & HV_SYNIC_SINT_MASKED;
108915e6f78SVitaly Kuznetsov 
109915e6f78SVitaly Kuznetsov 	/*
110915e6f78SVitaly Kuznetsov 	 * Valid vectors are 16-255, however, nested Hyper-V attempts to write
111915e6f78SVitaly Kuznetsov 	 * default '0x10000' value on boot and this should not #GP. We need to
112915e6f78SVitaly Kuznetsov 	 * allow zero-initing the register from host as well.
113915e6f78SVitaly Kuznetsov 	 */
114915e6f78SVitaly Kuznetsov 	if (vector < HV_SYNIC_FIRST_VALID_VECTOR && !host && !masked)
11598f65ad4SVitaly Kuznetsov 		return 1;
11698f65ad4SVitaly Kuznetsov 	/*
11798f65ad4SVitaly Kuznetsov 	 * Guest may configure multiple SINTs to use the same vector, so
11898f65ad4SVitaly Kuznetsov 	 * we maintain a bitmap of vectors handled by synic, and a
11998f65ad4SVitaly Kuznetsov 	 * bitmap of vectors with auto-eoi behavior.  The bitmaps are
12098f65ad4SVitaly Kuznetsov 	 * updated here, and atomically queried on fast paths.
12198f65ad4SVitaly Kuznetsov 	 */
12298f65ad4SVitaly Kuznetsov 	old_vector = synic_read_sint(synic, sint) & HV_SYNIC_SINT_VECTOR_MASK;
12398f65ad4SVitaly Kuznetsov 
12498f65ad4SVitaly Kuznetsov 	atomic64_set(&synic->sint[sint], data);
12598f65ad4SVitaly Kuznetsov 
12698f65ad4SVitaly Kuznetsov 	synic_update_vector(synic, old_vector);
12798f65ad4SVitaly Kuznetsov 
12898f65ad4SVitaly Kuznetsov 	synic_update_vector(synic, vector);
1295c919412SAndrey Smetanin 
1305c919412SAndrey Smetanin 	/* Load SynIC vectors into EOI exit bitmap */
1315c919412SAndrey Smetanin 	kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic));
1325c919412SAndrey Smetanin 	return 0;
1335c919412SAndrey Smetanin }
1345c919412SAndrey Smetanin 
135d3457c87SRoman Kagan static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx)
136d3457c87SRoman Kagan {
137d3457c87SRoman Kagan 	struct kvm_vcpu *vcpu = NULL;
138d3457c87SRoman Kagan 	int i;
139d3457c87SRoman Kagan 
1409170200eSVitaly Kuznetsov 	if (vpidx >= KVM_MAX_VCPUS)
1419170200eSVitaly Kuznetsov 		return NULL;
1429170200eSVitaly Kuznetsov 
143d3457c87SRoman Kagan 	vcpu = kvm_get_vcpu(kvm, vpidx);
144d3457c87SRoman Kagan 	if (vcpu && vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx)
145d3457c87SRoman Kagan 		return vcpu;
146d3457c87SRoman Kagan 	kvm_for_each_vcpu(i, vcpu, kvm)
147d3457c87SRoman Kagan 		if (vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx)
148d3457c87SRoman Kagan 			return vcpu;
149d3457c87SRoman Kagan 	return NULL;
150d3457c87SRoman Kagan }
151d3457c87SRoman Kagan 
152d3457c87SRoman Kagan static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vpidx)
1535c919412SAndrey Smetanin {
1545c919412SAndrey Smetanin 	struct kvm_vcpu *vcpu;
1555c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic;
1565c919412SAndrey Smetanin 
157d3457c87SRoman Kagan 	vcpu = get_vcpu_by_vpidx(kvm, vpidx);
1585c919412SAndrey Smetanin 	if (!vcpu)
1595c919412SAndrey Smetanin 		return NULL;
1605c919412SAndrey Smetanin 	synic = vcpu_to_synic(vcpu);
1615c919412SAndrey Smetanin 	return (synic->active) ? synic : NULL;
1625c919412SAndrey Smetanin }
1635c919412SAndrey Smetanin 
1645c919412SAndrey Smetanin static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
1655c919412SAndrey Smetanin {
1665c919412SAndrey Smetanin 	struct kvm *kvm = vcpu->kvm;
167765eaa0fSAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
1681f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
1691f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv_stimer *stimer;
17008a800acSVitaly Kuznetsov 	int gsi, idx;
1715c919412SAndrey Smetanin 
17218659a9cSAndrey Smetanin 	trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint);
1735c919412SAndrey Smetanin 
1741f4b34f8SAndrey Smetanin 	/* Try to deliver pending Hyper-V SynIC timers messages */
1751f4b34f8SAndrey Smetanin 	for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) {
1761f4b34f8SAndrey Smetanin 		stimer = &hv_vcpu->stimer[idx];
1776a058a1eSVitaly Kuznetsov 		if (stimer->msg_pending && stimer->config.enable &&
1788644f771SVitaly Kuznetsov 		    !stimer->config.direct_mode &&
17908a800acSVitaly Kuznetsov 		    stimer->config.sintx == sint)
18008a800acSVitaly Kuznetsov 			stimer_mark_pending(stimer, false);
1811f4b34f8SAndrey Smetanin 	}
1821f4b34f8SAndrey Smetanin 
1835c919412SAndrey Smetanin 	idx = srcu_read_lock(&kvm->irq_srcu);
1841f4b34f8SAndrey Smetanin 	gsi = atomic_read(&synic->sint_to_gsi[sint]);
1855c919412SAndrey Smetanin 	if (gsi != -1)
1865c919412SAndrey Smetanin 		kvm_notify_acked_gsi(kvm, gsi);
1875c919412SAndrey Smetanin 	srcu_read_unlock(&kvm->irq_srcu, idx);
1885c919412SAndrey Smetanin }
1895c919412SAndrey Smetanin 
190db397571SAndrey Smetanin static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
191db397571SAndrey Smetanin {
192db397571SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
193db397571SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
194db397571SAndrey Smetanin 
195db397571SAndrey Smetanin 	hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
196db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.msr = msr;
197db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.control = synic->control;
198db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
199db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
200db397571SAndrey Smetanin 
201db397571SAndrey Smetanin 	kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
202db397571SAndrey Smetanin }
203db397571SAndrey Smetanin 
2045c919412SAndrey Smetanin static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
2055c919412SAndrey Smetanin 			 u32 msr, u64 data, bool host)
2065c919412SAndrey Smetanin {
2075c919412SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
2085c919412SAndrey Smetanin 	int ret;
2095c919412SAndrey Smetanin 
21044883f01SPaolo Bonzini 	if (!synic->active && !host)
2115c919412SAndrey Smetanin 		return 1;
2125c919412SAndrey Smetanin 
21318659a9cSAndrey Smetanin 	trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host);
21418659a9cSAndrey Smetanin 
2155c919412SAndrey Smetanin 	ret = 0;
2165c919412SAndrey Smetanin 	switch (msr) {
2175c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
2185c919412SAndrey Smetanin 		synic->control = data;
219db397571SAndrey Smetanin 		if (!host)
220db397571SAndrey Smetanin 			synic_exit(synic, msr);
2215c919412SAndrey Smetanin 		break;
2225c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
2235c919412SAndrey Smetanin 		if (!host) {
2245c919412SAndrey Smetanin 			ret = 1;
2255c919412SAndrey Smetanin 			break;
2265c919412SAndrey Smetanin 		}
2275c919412SAndrey Smetanin 		synic->version = data;
2285c919412SAndrey Smetanin 		break;
2295c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
230efc479e6SRoman Kagan 		if ((data & HV_SYNIC_SIEFP_ENABLE) && !host &&
231efc479e6SRoman Kagan 		    !synic->dont_zero_synic_pages)
2325c919412SAndrey Smetanin 			if (kvm_clear_guest(vcpu->kvm,
2335c919412SAndrey Smetanin 					    data & PAGE_MASK, PAGE_SIZE)) {
2345c919412SAndrey Smetanin 				ret = 1;
2355c919412SAndrey Smetanin 				break;
2365c919412SAndrey Smetanin 			}
2375c919412SAndrey Smetanin 		synic->evt_page = data;
238db397571SAndrey Smetanin 		if (!host)
239db397571SAndrey Smetanin 			synic_exit(synic, msr);
2405c919412SAndrey Smetanin 		break;
2415c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
242efc479e6SRoman Kagan 		if ((data & HV_SYNIC_SIMP_ENABLE) && !host &&
243efc479e6SRoman Kagan 		    !synic->dont_zero_synic_pages)
2445c919412SAndrey Smetanin 			if (kvm_clear_guest(vcpu->kvm,
2455c919412SAndrey Smetanin 					    data & PAGE_MASK, PAGE_SIZE)) {
2465c919412SAndrey Smetanin 				ret = 1;
2475c919412SAndrey Smetanin 				break;
2485c919412SAndrey Smetanin 			}
2495c919412SAndrey Smetanin 		synic->msg_page = data;
250db397571SAndrey Smetanin 		if (!host)
251db397571SAndrey Smetanin 			synic_exit(synic, msr);
2525c919412SAndrey Smetanin 		break;
2535c919412SAndrey Smetanin 	case HV_X64_MSR_EOM: {
2545c919412SAndrey Smetanin 		int i;
2555c919412SAndrey Smetanin 
2565c919412SAndrey Smetanin 		for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
2575c919412SAndrey Smetanin 			kvm_hv_notify_acked_sint(vcpu, i);
2585c919412SAndrey Smetanin 		break;
2595c919412SAndrey Smetanin 	}
2605c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
2617be58a64SAndrey Smetanin 		ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host);
2625c919412SAndrey Smetanin 		break;
2635c919412SAndrey Smetanin 	default:
2645c919412SAndrey Smetanin 		ret = 1;
2655c919412SAndrey Smetanin 		break;
2665c919412SAndrey Smetanin 	}
2675c919412SAndrey Smetanin 	return ret;
2685c919412SAndrey Smetanin }
2695c919412SAndrey Smetanin 
27044883f01SPaolo Bonzini static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata,
27144883f01SPaolo Bonzini 			 bool host)
2725c919412SAndrey Smetanin {
2735c919412SAndrey Smetanin 	int ret;
2745c919412SAndrey Smetanin 
27544883f01SPaolo Bonzini 	if (!synic->active && !host)
2765c919412SAndrey Smetanin 		return 1;
2775c919412SAndrey Smetanin 
2785c919412SAndrey Smetanin 	ret = 0;
2795c919412SAndrey Smetanin 	switch (msr) {
2805c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
2815c919412SAndrey Smetanin 		*pdata = synic->control;
2825c919412SAndrey Smetanin 		break;
2835c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
2845c919412SAndrey Smetanin 		*pdata = synic->version;
2855c919412SAndrey Smetanin 		break;
2865c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
2875c919412SAndrey Smetanin 		*pdata = synic->evt_page;
2885c919412SAndrey Smetanin 		break;
2895c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
2905c919412SAndrey Smetanin 		*pdata = synic->msg_page;
2915c919412SAndrey Smetanin 		break;
2925c919412SAndrey Smetanin 	case HV_X64_MSR_EOM:
2935c919412SAndrey Smetanin 		*pdata = 0;
2945c919412SAndrey Smetanin 		break;
2955c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
2965c919412SAndrey Smetanin 		*pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
2975c919412SAndrey Smetanin 		break;
2985c919412SAndrey Smetanin 	default:
2995c919412SAndrey Smetanin 		ret = 1;
3005c919412SAndrey Smetanin 		break;
3015c919412SAndrey Smetanin 	}
3025c919412SAndrey Smetanin 	return ret;
3035c919412SAndrey Smetanin }
3045c919412SAndrey Smetanin 
305ecd8a8c2SJiang Biao static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
3065c919412SAndrey Smetanin {
3075c919412SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
3085c919412SAndrey Smetanin 	struct kvm_lapic_irq irq;
3095c919412SAndrey Smetanin 	int ret, vector;
3105c919412SAndrey Smetanin 
3115c919412SAndrey Smetanin 	if (sint >= ARRAY_SIZE(synic->sint))
3125c919412SAndrey Smetanin 		return -EINVAL;
3135c919412SAndrey Smetanin 
3145c919412SAndrey Smetanin 	vector = synic_get_sint_vector(synic_read_sint(synic, sint));
3155c919412SAndrey Smetanin 	if (vector < 0)
3165c919412SAndrey Smetanin 		return -ENOENT;
3175c919412SAndrey Smetanin 
3185c919412SAndrey Smetanin 	memset(&irq, 0, sizeof(irq));
319f98a3efbSRadim Krčmář 	irq.shorthand = APIC_DEST_SELF;
3205c919412SAndrey Smetanin 	irq.dest_mode = APIC_DEST_PHYSICAL;
3215c919412SAndrey Smetanin 	irq.delivery_mode = APIC_DM_FIXED;
3225c919412SAndrey Smetanin 	irq.vector = vector;
3235c919412SAndrey Smetanin 	irq.level = 1;
3245c919412SAndrey Smetanin 
325f98a3efbSRadim Krčmář 	ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq, NULL);
32618659a9cSAndrey Smetanin 	trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret);
3275c919412SAndrey Smetanin 	return ret;
3285c919412SAndrey Smetanin }
3295c919412SAndrey Smetanin 
330d3457c87SRoman Kagan int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vpidx, u32 sint)
3315c919412SAndrey Smetanin {
3325c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic;
3335c919412SAndrey Smetanin 
334d3457c87SRoman Kagan 	synic = synic_get(kvm, vpidx);
3355c919412SAndrey Smetanin 	if (!synic)
3365c919412SAndrey Smetanin 		return -EINVAL;
3375c919412SAndrey Smetanin 
3385c919412SAndrey Smetanin 	return synic_set_irq(synic, sint);
3395c919412SAndrey Smetanin }
3405c919412SAndrey Smetanin 
3415c919412SAndrey Smetanin void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
3425c919412SAndrey Smetanin {
3435c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
3445c919412SAndrey Smetanin 	int i;
3455c919412SAndrey Smetanin 
34618659a9cSAndrey Smetanin 	trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector);
3475c919412SAndrey Smetanin 
3485c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
3495c919412SAndrey Smetanin 		if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
3505c919412SAndrey Smetanin 			kvm_hv_notify_acked_sint(vcpu, i);
3515c919412SAndrey Smetanin }
3525c919412SAndrey Smetanin 
353d3457c87SRoman Kagan static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vpidx, u32 sint, int gsi)
3545c919412SAndrey Smetanin {
3555c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic;
3565c919412SAndrey Smetanin 
357d3457c87SRoman Kagan 	synic = synic_get(kvm, vpidx);
3585c919412SAndrey Smetanin 	if (!synic)
3595c919412SAndrey Smetanin 		return -EINVAL;
3605c919412SAndrey Smetanin 
3615c919412SAndrey Smetanin 	if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
3625c919412SAndrey Smetanin 		return -EINVAL;
3635c919412SAndrey Smetanin 
3645c919412SAndrey Smetanin 	atomic_set(&synic->sint_to_gsi[sint], gsi);
3655c919412SAndrey Smetanin 	return 0;
3665c919412SAndrey Smetanin }
3675c919412SAndrey Smetanin 
3685c919412SAndrey Smetanin void kvm_hv_irq_routing_update(struct kvm *kvm)
3695c919412SAndrey Smetanin {
3705c919412SAndrey Smetanin 	struct kvm_irq_routing_table *irq_rt;
3715c919412SAndrey Smetanin 	struct kvm_kernel_irq_routing_entry *e;
3725c919412SAndrey Smetanin 	u32 gsi;
3735c919412SAndrey Smetanin 
3745c919412SAndrey Smetanin 	irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
3755c919412SAndrey Smetanin 					lockdep_is_held(&kvm->irq_lock));
3765c919412SAndrey Smetanin 
3775c919412SAndrey Smetanin 	for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
3785c919412SAndrey Smetanin 		hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
3795c919412SAndrey Smetanin 			if (e->type == KVM_IRQ_ROUTING_HV_SINT)
3805c919412SAndrey Smetanin 				kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
3815c919412SAndrey Smetanin 						    e->hv_sint.sint, gsi);
3825c919412SAndrey Smetanin 		}
3835c919412SAndrey Smetanin 	}
3845c919412SAndrey Smetanin }
3855c919412SAndrey Smetanin 
3865c919412SAndrey Smetanin static void synic_init(struct kvm_vcpu_hv_synic *synic)
3875c919412SAndrey Smetanin {
3885c919412SAndrey Smetanin 	int i;
3895c919412SAndrey Smetanin 
3905c919412SAndrey Smetanin 	memset(synic, 0, sizeof(*synic));
3915c919412SAndrey Smetanin 	synic->version = HV_SYNIC_VERSION_1;
3925c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
3935c919412SAndrey Smetanin 		atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
3945c919412SAndrey Smetanin 		atomic_set(&synic->sint_to_gsi[i], -1);
3955c919412SAndrey Smetanin 	}
3965c919412SAndrey Smetanin }
3975c919412SAndrey Smetanin 
39893bf4172SAndrey Smetanin static u64 get_time_ref_counter(struct kvm *kvm)
39993bf4172SAndrey Smetanin {
400095cf55dSPaolo Bonzini 	struct kvm_hv *hv = &kvm->arch.hyperv;
401095cf55dSPaolo Bonzini 	struct kvm_vcpu *vcpu;
402095cf55dSPaolo Bonzini 	u64 tsc;
403095cf55dSPaolo Bonzini 
404095cf55dSPaolo Bonzini 	/*
405095cf55dSPaolo Bonzini 	 * The guest has not set up the TSC page or the clock isn't
406095cf55dSPaolo Bonzini 	 * stable, fall back to get_kvmclock_ns.
407095cf55dSPaolo Bonzini 	 */
408095cf55dSPaolo Bonzini 	if (!hv->tsc_ref.tsc_sequence)
409108b249cSPaolo Bonzini 		return div_u64(get_kvmclock_ns(kvm), 100);
410095cf55dSPaolo Bonzini 
411095cf55dSPaolo Bonzini 	vcpu = kvm_get_vcpu(kvm, 0);
412095cf55dSPaolo Bonzini 	tsc = kvm_read_l1_tsc(vcpu, rdtsc());
413095cf55dSPaolo Bonzini 	return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64)
414095cf55dSPaolo Bonzini 		+ hv->tsc_ref.tsc_offset;
41593bf4172SAndrey Smetanin }
41693bf4172SAndrey Smetanin 
417f3b138c5SAndrey Smetanin static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
4181f4b34f8SAndrey Smetanin 				bool vcpu_kick)
4191f4b34f8SAndrey Smetanin {
4201f4b34f8SAndrey Smetanin 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
4211f4b34f8SAndrey Smetanin 
4221f4b34f8SAndrey Smetanin 	set_bit(stimer->index,
4231f4b34f8SAndrey Smetanin 		vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
4241f4b34f8SAndrey Smetanin 	kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
4251f4b34f8SAndrey Smetanin 	if (vcpu_kick)
4261f4b34f8SAndrey Smetanin 		kvm_vcpu_kick(vcpu);
4271f4b34f8SAndrey Smetanin }
4281f4b34f8SAndrey Smetanin 
4291f4b34f8SAndrey Smetanin static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
4301f4b34f8SAndrey Smetanin {
4311f4b34f8SAndrey Smetanin 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
4321f4b34f8SAndrey Smetanin 
433ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_cleanup(stimer_to_vcpu(stimer)->vcpu_id,
434ac3e5fcaSAndrey Smetanin 				    stimer->index);
435ac3e5fcaSAndrey Smetanin 
436019b9781SAndrey Smetanin 	hrtimer_cancel(&stimer->timer);
4371f4b34f8SAndrey Smetanin 	clear_bit(stimer->index,
4381f4b34f8SAndrey Smetanin 		  vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
4391f4b34f8SAndrey Smetanin 	stimer->msg_pending = false;
440f808495dSAndrey Smetanin 	stimer->exp_time = 0;
4411f4b34f8SAndrey Smetanin }
4421f4b34f8SAndrey Smetanin 
4431f4b34f8SAndrey Smetanin static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
4441f4b34f8SAndrey Smetanin {
4451f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv_stimer *stimer;
4461f4b34f8SAndrey Smetanin 
4471f4b34f8SAndrey Smetanin 	stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
448ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_callback(stimer_to_vcpu(stimer)->vcpu_id,
449ac3e5fcaSAndrey Smetanin 				     stimer->index);
450f3b138c5SAndrey Smetanin 	stimer_mark_pending(stimer, true);
4511f4b34f8SAndrey Smetanin 
4521f4b34f8SAndrey Smetanin 	return HRTIMER_NORESTART;
4531f4b34f8SAndrey Smetanin }
4541f4b34f8SAndrey Smetanin 
455f808495dSAndrey Smetanin /*
456f808495dSAndrey Smetanin  * stimer_start() assumptions:
457f808495dSAndrey Smetanin  * a) stimer->count is not equal to 0
458f808495dSAndrey Smetanin  * b) stimer->config has HV_STIMER_ENABLE flag
459f808495dSAndrey Smetanin  */
4601f4b34f8SAndrey Smetanin static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
4611f4b34f8SAndrey Smetanin {
4621f4b34f8SAndrey Smetanin 	u64 time_now;
4631f4b34f8SAndrey Smetanin 	ktime_t ktime_now;
4641f4b34f8SAndrey Smetanin 
4651f4b34f8SAndrey Smetanin 	time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm);
4661f4b34f8SAndrey Smetanin 	ktime_now = ktime_get();
4671f4b34f8SAndrey Smetanin 
4686a058a1eSVitaly Kuznetsov 	if (stimer->config.periodic) {
469f808495dSAndrey Smetanin 		if (stimer->exp_time) {
470f808495dSAndrey Smetanin 			if (time_now >= stimer->exp_time) {
471f808495dSAndrey Smetanin 				u64 remainder;
4721f4b34f8SAndrey Smetanin 
473f808495dSAndrey Smetanin 				div64_u64_rem(time_now - stimer->exp_time,
474f808495dSAndrey Smetanin 					      stimer->count, &remainder);
475f808495dSAndrey Smetanin 				stimer->exp_time =
476f808495dSAndrey Smetanin 					time_now + (stimer->count - remainder);
477f808495dSAndrey Smetanin 			}
478f808495dSAndrey Smetanin 		} else
4791f4b34f8SAndrey Smetanin 			stimer->exp_time = time_now + stimer->count;
480f808495dSAndrey Smetanin 
481ac3e5fcaSAndrey Smetanin 		trace_kvm_hv_stimer_start_periodic(
482ac3e5fcaSAndrey Smetanin 					stimer_to_vcpu(stimer)->vcpu_id,
483ac3e5fcaSAndrey Smetanin 					stimer->index,
484ac3e5fcaSAndrey Smetanin 					time_now, stimer->exp_time);
485ac3e5fcaSAndrey Smetanin 
4861f4b34f8SAndrey Smetanin 		hrtimer_start(&stimer->timer,
487f808495dSAndrey Smetanin 			      ktime_add_ns(ktime_now,
488f808495dSAndrey Smetanin 					   100 * (stimer->exp_time - time_now)),
4891f4b34f8SAndrey Smetanin 			      HRTIMER_MODE_ABS);
4901f4b34f8SAndrey Smetanin 		return 0;
4911f4b34f8SAndrey Smetanin 	}
4921f4b34f8SAndrey Smetanin 	stimer->exp_time = stimer->count;
4931f4b34f8SAndrey Smetanin 	if (time_now >= stimer->count) {
4941f4b34f8SAndrey Smetanin 		/*
4951f4b34f8SAndrey Smetanin 		 * Expire timer according to Hypervisor Top-Level Functional
4961f4b34f8SAndrey Smetanin 		 * specification v4(15.3.1):
4971f4b34f8SAndrey Smetanin 		 * "If a one shot is enabled and the specified count is in
4981f4b34f8SAndrey Smetanin 		 * the past, it will expire immediately."
4991f4b34f8SAndrey Smetanin 		 */
500f3b138c5SAndrey Smetanin 		stimer_mark_pending(stimer, false);
5011f4b34f8SAndrey Smetanin 		return 0;
5021f4b34f8SAndrey Smetanin 	}
5031f4b34f8SAndrey Smetanin 
504ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_start_one_shot(stimer_to_vcpu(stimer)->vcpu_id,
505ac3e5fcaSAndrey Smetanin 					   stimer->index,
506ac3e5fcaSAndrey Smetanin 					   time_now, stimer->count);
507ac3e5fcaSAndrey Smetanin 
5081f4b34f8SAndrey Smetanin 	hrtimer_start(&stimer->timer,
5091f4b34f8SAndrey Smetanin 		      ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
5101f4b34f8SAndrey Smetanin 		      HRTIMER_MODE_ABS);
5111f4b34f8SAndrey Smetanin 	return 0;
5121f4b34f8SAndrey Smetanin }
5131f4b34f8SAndrey Smetanin 
5141f4b34f8SAndrey Smetanin static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
5151f4b34f8SAndrey Smetanin 			     bool host)
5161f4b34f8SAndrey Smetanin {
5178644f771SVitaly Kuznetsov 	union hv_stimer_config new_config = {.as_uint64 = config},
5188644f771SVitaly Kuznetsov 		old_config = {.as_uint64 = stimer->config.as_uint64};
5196a058a1eSVitaly Kuznetsov 
520ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_set_config(stimer_to_vcpu(stimer)->vcpu_id,
521ac3e5fcaSAndrey Smetanin 				       stimer->index, config, host);
522ac3e5fcaSAndrey Smetanin 
523f3b138c5SAndrey Smetanin 	stimer_cleanup(stimer);
5248644f771SVitaly Kuznetsov 	if (old_config.enable &&
5258644f771SVitaly Kuznetsov 	    !new_config.direct_mode && new_config.sintx == 0)
5266a058a1eSVitaly Kuznetsov 		new_config.enable = 0;
5276a058a1eSVitaly Kuznetsov 	stimer->config.as_uint64 = new_config.as_uint64;
5288644f771SVitaly Kuznetsov 
529f3b138c5SAndrey Smetanin 	stimer_mark_pending(stimer, false);
5301f4b34f8SAndrey Smetanin 	return 0;
5311f4b34f8SAndrey Smetanin }
5321f4b34f8SAndrey Smetanin 
5331f4b34f8SAndrey Smetanin static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
5341f4b34f8SAndrey Smetanin 			    bool host)
5351f4b34f8SAndrey Smetanin {
536ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_set_count(stimer_to_vcpu(stimer)->vcpu_id,
537ac3e5fcaSAndrey Smetanin 				      stimer->index, count, host);
538ac3e5fcaSAndrey Smetanin 
5391f4b34f8SAndrey Smetanin 	stimer_cleanup(stimer);
540f3b138c5SAndrey Smetanin 	stimer->count = count;
5411f4b34f8SAndrey Smetanin 	if (stimer->count == 0)
5426a058a1eSVitaly Kuznetsov 		stimer->config.enable = 0;
5436a058a1eSVitaly Kuznetsov 	else if (stimer->config.auto_enable)
5446a058a1eSVitaly Kuznetsov 		stimer->config.enable = 1;
545f3b138c5SAndrey Smetanin 	stimer_mark_pending(stimer, false);
5461f4b34f8SAndrey Smetanin 	return 0;
5471f4b34f8SAndrey Smetanin }
5481f4b34f8SAndrey Smetanin 
5491f4b34f8SAndrey Smetanin static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
5501f4b34f8SAndrey Smetanin {
5516a058a1eSVitaly Kuznetsov 	*pconfig = stimer->config.as_uint64;
5521f4b34f8SAndrey Smetanin 	return 0;
5531f4b34f8SAndrey Smetanin }
5541f4b34f8SAndrey Smetanin 
5551f4b34f8SAndrey Smetanin static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
5561f4b34f8SAndrey Smetanin {
5571f4b34f8SAndrey Smetanin 	*pcount = stimer->count;
5581f4b34f8SAndrey Smetanin 	return 0;
5591f4b34f8SAndrey Smetanin }
5601f4b34f8SAndrey Smetanin 
5611f4b34f8SAndrey Smetanin static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
5627deec5e0SRoman Kagan 			     struct hv_message *src_msg, bool no_retry)
5631f4b34f8SAndrey Smetanin {
5641f4b34f8SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
5653a0e7731SRoman Kagan 	int msg_off = offsetof(struct hv_message_page, sint_message[sint]);
5663a0e7731SRoman Kagan 	gfn_t msg_page_gfn;
5673a0e7731SRoman Kagan 	struct hv_message_header hv_hdr;
5681f4b34f8SAndrey Smetanin 	int r;
5691f4b34f8SAndrey Smetanin 
5701f4b34f8SAndrey Smetanin 	if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
5711f4b34f8SAndrey Smetanin 		return -ENOENT;
5721f4b34f8SAndrey Smetanin 
5733a0e7731SRoman Kagan 	msg_page_gfn = synic->msg_page >> PAGE_SHIFT;
5741f4b34f8SAndrey Smetanin 
5753a0e7731SRoman Kagan 	/*
5763a0e7731SRoman Kagan 	 * Strictly following the spec-mandated ordering would assume setting
5773a0e7731SRoman Kagan 	 * .msg_pending before checking .message_type.  However, this function
5783a0e7731SRoman Kagan 	 * is only called in vcpu context so the entire update is atomic from
5793a0e7731SRoman Kagan 	 * guest POV and thus the exact order here doesn't matter.
5803a0e7731SRoman Kagan 	 */
5813a0e7731SRoman Kagan 	r = kvm_vcpu_read_guest_page(vcpu, msg_page_gfn, &hv_hdr.message_type,
5823a0e7731SRoman Kagan 				     msg_off + offsetof(struct hv_message,
5833a0e7731SRoman Kagan 							header.message_type),
5843a0e7731SRoman Kagan 				     sizeof(hv_hdr.message_type));
5853a0e7731SRoman Kagan 	if (r < 0)
5861f4b34f8SAndrey Smetanin 		return r;
5873a0e7731SRoman Kagan 
5883a0e7731SRoman Kagan 	if (hv_hdr.message_type != HVMSG_NONE) {
5897deec5e0SRoman Kagan 		if (no_retry)
5907deec5e0SRoman Kagan 			return 0;
5917deec5e0SRoman Kagan 
5923a0e7731SRoman Kagan 		hv_hdr.message_flags.msg_pending = 1;
5933a0e7731SRoman Kagan 		r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn,
5943a0e7731SRoman Kagan 					      &hv_hdr.message_flags,
5953a0e7731SRoman Kagan 					      msg_off +
5963a0e7731SRoman Kagan 					      offsetof(struct hv_message,
5973a0e7731SRoman Kagan 						       header.message_flags),
5983a0e7731SRoman Kagan 					      sizeof(hv_hdr.message_flags));
5993a0e7731SRoman Kagan 		if (r < 0)
6003a0e7731SRoman Kagan 			return r;
6013a0e7731SRoman Kagan 		return -EAGAIN;
6023a0e7731SRoman Kagan 	}
6033a0e7731SRoman Kagan 
6043a0e7731SRoman Kagan 	r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn, src_msg, msg_off,
6053a0e7731SRoman Kagan 				      sizeof(src_msg->header) +
6063a0e7731SRoman Kagan 				      src_msg->header.payload_size);
6073a0e7731SRoman Kagan 	if (r < 0)
6083a0e7731SRoman Kagan 		return r;
6093a0e7731SRoman Kagan 
6103a0e7731SRoman Kagan 	r = synic_set_irq(synic, sint);
6113a0e7731SRoman Kagan 	if (r < 0)
6123a0e7731SRoman Kagan 		return r;
6133a0e7731SRoman Kagan 	if (r == 0)
6143a0e7731SRoman Kagan 		return -EFAULT;
6153a0e7731SRoman Kagan 	return 0;
6161f4b34f8SAndrey Smetanin }
6171f4b34f8SAndrey Smetanin 
6180cdeabb1SAndrey Smetanin static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
6191f4b34f8SAndrey Smetanin {
6201f4b34f8SAndrey Smetanin 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
6211f4b34f8SAndrey Smetanin 	struct hv_message *msg = &stimer->msg;
6221f4b34f8SAndrey Smetanin 	struct hv_timer_message_payload *payload =
6231f4b34f8SAndrey Smetanin 			(struct hv_timer_message_payload *)&msg->u.payload;
6241f4b34f8SAndrey Smetanin 
6257deec5e0SRoman Kagan 	/*
6267deec5e0SRoman Kagan 	 * To avoid piling up periodic ticks, don't retry message
6277deec5e0SRoman Kagan 	 * delivery for them (within "lazy" lost ticks policy).
6287deec5e0SRoman Kagan 	 */
6296a058a1eSVitaly Kuznetsov 	bool no_retry = stimer->config.periodic;
6307deec5e0SRoman Kagan 
6311f4b34f8SAndrey Smetanin 	payload->expiration_time = stimer->exp_time;
6321f4b34f8SAndrey Smetanin 	payload->delivery_time = get_time_ref_counter(vcpu->kvm);
6330cdeabb1SAndrey Smetanin 	return synic_deliver_msg(vcpu_to_synic(vcpu),
6346a058a1eSVitaly Kuznetsov 				 stimer->config.sintx, msg,
6357deec5e0SRoman Kagan 				 no_retry);
6361f4b34f8SAndrey Smetanin }
6371f4b34f8SAndrey Smetanin 
6388644f771SVitaly Kuznetsov static int stimer_notify_direct(struct kvm_vcpu_hv_stimer *stimer)
6398644f771SVitaly Kuznetsov {
6408644f771SVitaly Kuznetsov 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
6418644f771SVitaly Kuznetsov 	struct kvm_lapic_irq irq = {
6428644f771SVitaly Kuznetsov 		.delivery_mode = APIC_DM_FIXED,
6438644f771SVitaly Kuznetsov 		.vector = stimer->config.apic_vector
6448644f771SVitaly Kuznetsov 	};
6458644f771SVitaly Kuznetsov 
6468644f771SVitaly Kuznetsov 	return !kvm_apic_set_irq(vcpu, &irq, NULL);
6478644f771SVitaly Kuznetsov }
6488644f771SVitaly Kuznetsov 
6491f4b34f8SAndrey Smetanin static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
6501f4b34f8SAndrey Smetanin {
6518644f771SVitaly Kuznetsov 	int r, direct = stimer->config.direct_mode;
652ac3e5fcaSAndrey Smetanin 
6530cdeabb1SAndrey Smetanin 	stimer->msg_pending = true;
6548644f771SVitaly Kuznetsov 	if (!direct)
655ac3e5fcaSAndrey Smetanin 		r = stimer_send_msg(stimer);
6568644f771SVitaly Kuznetsov 	else
6578644f771SVitaly Kuznetsov 		r = stimer_notify_direct(stimer);
658ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_expiration(stimer_to_vcpu(stimer)->vcpu_id,
6598644f771SVitaly Kuznetsov 				       stimer->index, direct, r);
660ac3e5fcaSAndrey Smetanin 	if (!r) {
6610cdeabb1SAndrey Smetanin 		stimer->msg_pending = false;
6626a058a1eSVitaly Kuznetsov 		if (!(stimer->config.periodic))
6636a058a1eSVitaly Kuznetsov 			stimer->config.enable = 0;
6640cdeabb1SAndrey Smetanin 	}
6651f4b34f8SAndrey Smetanin }
6661f4b34f8SAndrey Smetanin 
6671f4b34f8SAndrey Smetanin void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
6681f4b34f8SAndrey Smetanin {
6691f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
6701f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv_stimer *stimer;
671f3b138c5SAndrey Smetanin 	u64 time_now, exp_time;
6721f4b34f8SAndrey Smetanin 	int i;
6731f4b34f8SAndrey Smetanin 
6741f4b34f8SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
6751f4b34f8SAndrey Smetanin 		if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
6761f4b34f8SAndrey Smetanin 			stimer = &hv_vcpu->stimer[i];
6776a058a1eSVitaly Kuznetsov 			if (stimer->config.enable) {
678f3b138c5SAndrey Smetanin 				exp_time = stimer->exp_time;
6790cdeabb1SAndrey Smetanin 
680f3b138c5SAndrey Smetanin 				if (exp_time) {
681f3b138c5SAndrey Smetanin 					time_now =
682f3b138c5SAndrey Smetanin 						get_time_ref_counter(vcpu->kvm);
683f3b138c5SAndrey Smetanin 					if (time_now >= exp_time)
684f3b138c5SAndrey Smetanin 						stimer_expiration(stimer);
685f3b138c5SAndrey Smetanin 				}
686f3b138c5SAndrey Smetanin 
6876a058a1eSVitaly Kuznetsov 				if ((stimer->config.enable) &&
688f1ff89ecSRoman Kagan 				    stimer->count) {
689f1ff89ecSRoman Kagan 					if (!stimer->msg_pending)
6900cdeabb1SAndrey Smetanin 						stimer_start(stimer);
691f1ff89ecSRoman Kagan 				} else
6920cdeabb1SAndrey Smetanin 					stimer_cleanup(stimer);
6931f4b34f8SAndrey Smetanin 			}
6941f4b34f8SAndrey Smetanin 		}
6951f4b34f8SAndrey Smetanin }
6961f4b34f8SAndrey Smetanin 
6971f4b34f8SAndrey Smetanin void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)
6981f4b34f8SAndrey Smetanin {
6991f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
7001f4b34f8SAndrey Smetanin 	int i;
7011f4b34f8SAndrey Smetanin 
7021f4b34f8SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
7031f4b34f8SAndrey Smetanin 		stimer_cleanup(&hv_vcpu->stimer[i]);
7041f4b34f8SAndrey Smetanin }
7051f4b34f8SAndrey Smetanin 
70672bbf935SLadi Prosek bool kvm_hv_assist_page_enabled(struct kvm_vcpu *vcpu)
70772bbf935SLadi Prosek {
70872bbf935SLadi Prosek 	if (!(vcpu->arch.hyperv.hv_vapic & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE))
70972bbf935SLadi Prosek 		return false;
71072bbf935SLadi Prosek 	return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
71172bbf935SLadi Prosek }
71272bbf935SLadi Prosek EXPORT_SYMBOL_GPL(kvm_hv_assist_page_enabled);
71372bbf935SLadi Prosek 
71472bbf935SLadi Prosek bool kvm_hv_get_assist_page(struct kvm_vcpu *vcpu,
71572bbf935SLadi Prosek 			    struct hv_vp_assist_page *assist_page)
71672bbf935SLadi Prosek {
71772bbf935SLadi Prosek 	if (!kvm_hv_assist_page_enabled(vcpu))
71872bbf935SLadi Prosek 		return false;
71972bbf935SLadi Prosek 	return !kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data,
72072bbf935SLadi Prosek 				      assist_page, sizeof(*assist_page));
72172bbf935SLadi Prosek }
72272bbf935SLadi Prosek EXPORT_SYMBOL_GPL(kvm_hv_get_assist_page);
72372bbf935SLadi Prosek 
7241f4b34f8SAndrey Smetanin static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer)
7251f4b34f8SAndrey Smetanin {
7261f4b34f8SAndrey Smetanin 	struct hv_message *msg = &stimer->msg;
7271f4b34f8SAndrey Smetanin 	struct hv_timer_message_payload *payload =
7281f4b34f8SAndrey Smetanin 			(struct hv_timer_message_payload *)&msg->u.payload;
7291f4b34f8SAndrey Smetanin 
7301f4b34f8SAndrey Smetanin 	memset(&msg->header, 0, sizeof(msg->header));
7311f4b34f8SAndrey Smetanin 	msg->header.message_type = HVMSG_TIMER_EXPIRED;
7321f4b34f8SAndrey Smetanin 	msg->header.payload_size = sizeof(*payload);
7331f4b34f8SAndrey Smetanin 
7341f4b34f8SAndrey Smetanin 	payload->timer_index = stimer->index;
7351f4b34f8SAndrey Smetanin 	payload->expiration_time = 0;
7361f4b34f8SAndrey Smetanin 	payload->delivery_time = 0;
7371f4b34f8SAndrey Smetanin }
7381f4b34f8SAndrey Smetanin 
7391f4b34f8SAndrey Smetanin static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index)
7401f4b34f8SAndrey Smetanin {
7411f4b34f8SAndrey Smetanin 	memset(stimer, 0, sizeof(*stimer));
7421f4b34f8SAndrey Smetanin 	stimer->index = timer_index;
7431f4b34f8SAndrey Smetanin 	hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
7441f4b34f8SAndrey Smetanin 	stimer->timer.function = stimer_timer_callback;
7451f4b34f8SAndrey Smetanin 	stimer_prepare_msg(stimer);
7461f4b34f8SAndrey Smetanin }
7471f4b34f8SAndrey Smetanin 
7485c919412SAndrey Smetanin void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
7495c919412SAndrey Smetanin {
7501f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
7511f4b34f8SAndrey Smetanin 	int i;
7521f4b34f8SAndrey Smetanin 
7531f4b34f8SAndrey Smetanin 	synic_init(&hv_vcpu->synic);
7541f4b34f8SAndrey Smetanin 
7551f4b34f8SAndrey Smetanin 	bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
7561f4b34f8SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
7571f4b34f8SAndrey Smetanin 		stimer_init(&hv_vcpu->stimer[i], i);
7585c919412SAndrey Smetanin }
7595c919412SAndrey Smetanin 
760d3457c87SRoman Kagan void kvm_hv_vcpu_postcreate(struct kvm_vcpu *vcpu)
761d3457c87SRoman Kagan {
762d3457c87SRoman Kagan 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
763d3457c87SRoman Kagan 
764d3457c87SRoman Kagan 	hv_vcpu->vp_index = kvm_vcpu_get_idx(vcpu);
765d3457c87SRoman Kagan }
766d3457c87SRoman Kagan 
767efc479e6SRoman Kagan int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages)
7685c919412SAndrey Smetanin {
769efc479e6SRoman Kagan 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
770efc479e6SRoman Kagan 
7715c919412SAndrey Smetanin 	/*
7725c919412SAndrey Smetanin 	 * Hyper-V SynIC auto EOI SINT's are
7735c919412SAndrey Smetanin 	 * not compatible with APICV, so deactivate APICV
7745c919412SAndrey Smetanin 	 */
7755c919412SAndrey Smetanin 	kvm_vcpu_deactivate_apicv(vcpu);
776efc479e6SRoman Kagan 	synic->active = true;
777efc479e6SRoman Kagan 	synic->dont_zero_synic_pages = dont_zero_synic_pages;
7785c919412SAndrey Smetanin 	return 0;
7795c919412SAndrey Smetanin }
7805c919412SAndrey Smetanin 
781e83d5887SAndrey Smetanin static bool kvm_hv_msr_partition_wide(u32 msr)
782e83d5887SAndrey Smetanin {
783e83d5887SAndrey Smetanin 	bool r = false;
784e83d5887SAndrey Smetanin 
785e83d5887SAndrey Smetanin 	switch (msr) {
786e83d5887SAndrey Smetanin 	case HV_X64_MSR_GUEST_OS_ID:
787e83d5887SAndrey Smetanin 	case HV_X64_MSR_HYPERCALL:
788e83d5887SAndrey Smetanin 	case HV_X64_MSR_REFERENCE_TSC:
789e83d5887SAndrey Smetanin 	case HV_X64_MSR_TIME_REF_COUNT:
790e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_CTL:
791e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
792e516cebbSAndrey Smetanin 	case HV_X64_MSR_RESET:
793a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
794a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_TSC_EMULATION_CONTROL:
795a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_TSC_EMULATION_STATUS:
796e83d5887SAndrey Smetanin 		r = true;
797e83d5887SAndrey Smetanin 		break;
798e83d5887SAndrey Smetanin 	}
799e83d5887SAndrey Smetanin 
800e83d5887SAndrey Smetanin 	return r;
801e83d5887SAndrey Smetanin }
802e83d5887SAndrey Smetanin 
803e7d9513bSAndrey Smetanin static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
804e7d9513bSAndrey Smetanin 				     u32 index, u64 *pdata)
805e7d9513bSAndrey Smetanin {
806e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
807e7d9513bSAndrey Smetanin 
808e7d9513bSAndrey Smetanin 	if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
809e7d9513bSAndrey Smetanin 		return -EINVAL;
810e7d9513bSAndrey Smetanin 
811e7d9513bSAndrey Smetanin 	*pdata = hv->hv_crash_param[index];
812e7d9513bSAndrey Smetanin 	return 0;
813e7d9513bSAndrey Smetanin }
814e7d9513bSAndrey Smetanin 
815e7d9513bSAndrey Smetanin static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata)
816e7d9513bSAndrey Smetanin {
817e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
818e7d9513bSAndrey Smetanin 
819e7d9513bSAndrey Smetanin 	*pdata = hv->hv_crash_ctl;
820e7d9513bSAndrey Smetanin 	return 0;
821e7d9513bSAndrey Smetanin }
822e7d9513bSAndrey Smetanin 
823e7d9513bSAndrey Smetanin static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host)
824e7d9513bSAndrey Smetanin {
825e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
826e7d9513bSAndrey Smetanin 
827e7d9513bSAndrey Smetanin 	if (host)
828a4987defSVitaly Kuznetsov 		hv->hv_crash_ctl = data & HV_CRASH_CTL_CRASH_NOTIFY;
829e7d9513bSAndrey Smetanin 
830a4987defSVitaly Kuznetsov 	if (!host && (data & HV_CRASH_CTL_CRASH_NOTIFY)) {
831e7d9513bSAndrey Smetanin 
832e7d9513bSAndrey Smetanin 		vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
833e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[0],
834e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[1],
835e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[2],
836e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[3],
837e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[4]);
838e7d9513bSAndrey Smetanin 
839e7d9513bSAndrey Smetanin 		/* Send notification about crash to user space */
840e7d9513bSAndrey Smetanin 		kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
841e7d9513bSAndrey Smetanin 	}
842e7d9513bSAndrey Smetanin 
843e7d9513bSAndrey Smetanin 	return 0;
844e7d9513bSAndrey Smetanin }
845e7d9513bSAndrey Smetanin 
846e7d9513bSAndrey Smetanin static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
847e7d9513bSAndrey Smetanin 				     u32 index, u64 data)
848e7d9513bSAndrey Smetanin {
849e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
850e7d9513bSAndrey Smetanin 
851e7d9513bSAndrey Smetanin 	if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
852e7d9513bSAndrey Smetanin 		return -EINVAL;
853e7d9513bSAndrey Smetanin 
854e7d9513bSAndrey Smetanin 	hv->hv_crash_param[index] = data;
855e7d9513bSAndrey Smetanin 	return 0;
856e7d9513bSAndrey Smetanin }
857e7d9513bSAndrey Smetanin 
858095cf55dSPaolo Bonzini /*
859095cf55dSPaolo Bonzini  * The kvmclock and Hyper-V TSC page use similar formulas, and converting
860095cf55dSPaolo Bonzini  * between them is possible:
861095cf55dSPaolo Bonzini  *
862095cf55dSPaolo Bonzini  * kvmclock formula:
863095cf55dSPaolo Bonzini  *    nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32)
864095cf55dSPaolo Bonzini  *           + system_time
865095cf55dSPaolo Bonzini  *
866095cf55dSPaolo Bonzini  * Hyper-V formula:
867095cf55dSPaolo Bonzini  *    nsec/100 = ticks * scale / 2^64 + offset
868095cf55dSPaolo Bonzini  *
869095cf55dSPaolo Bonzini  * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula.
870095cf55dSPaolo Bonzini  * By dividing the kvmclock formula by 100 and equating what's left we get:
871095cf55dSPaolo Bonzini  *    ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
872095cf55dSPaolo Bonzini  *            scale / 2^64 =         tsc_to_system_mul * 2^(tsc_shift-32) / 100
873095cf55dSPaolo Bonzini  *            scale        =         tsc_to_system_mul * 2^(32+tsc_shift) / 100
874095cf55dSPaolo Bonzini  *
875095cf55dSPaolo Bonzini  * Now expand the kvmclock formula and divide by 100:
876095cf55dSPaolo Bonzini  *    nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32)
877095cf55dSPaolo Bonzini  *           - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32)
878095cf55dSPaolo Bonzini  *           + system_time
879095cf55dSPaolo Bonzini  *    nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
880095cf55dSPaolo Bonzini  *               - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100
881095cf55dSPaolo Bonzini  *               + system_time / 100
882095cf55dSPaolo Bonzini  *
883095cf55dSPaolo Bonzini  * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64:
884095cf55dSPaolo Bonzini  *    nsec/100 = ticks * scale / 2^64
885095cf55dSPaolo Bonzini  *               - tsc_timestamp * scale / 2^64
886095cf55dSPaolo Bonzini  *               + system_time / 100
887095cf55dSPaolo Bonzini  *
888095cf55dSPaolo Bonzini  * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out:
889095cf55dSPaolo Bonzini  *    offset = system_time / 100 - tsc_timestamp * scale / 2^64
890095cf55dSPaolo Bonzini  *
891095cf55dSPaolo Bonzini  * These two equivalencies are implemented in this function.
892095cf55dSPaolo Bonzini  */
893095cf55dSPaolo Bonzini static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock,
894095cf55dSPaolo Bonzini 					HV_REFERENCE_TSC_PAGE *tsc_ref)
895095cf55dSPaolo Bonzini {
896095cf55dSPaolo Bonzini 	u64 max_mul;
897095cf55dSPaolo Bonzini 
898095cf55dSPaolo Bonzini 	if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT))
899095cf55dSPaolo Bonzini 		return false;
900095cf55dSPaolo Bonzini 
901095cf55dSPaolo Bonzini 	/*
902095cf55dSPaolo Bonzini 	 * check if scale would overflow, if so we use the time ref counter
903095cf55dSPaolo Bonzini 	 *    tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64
904095cf55dSPaolo Bonzini 	 *    tsc_to_system_mul / 100 >= 2^(32-tsc_shift)
905095cf55dSPaolo Bonzini 	 *    tsc_to_system_mul >= 100 * 2^(32-tsc_shift)
906095cf55dSPaolo Bonzini 	 */
907095cf55dSPaolo Bonzini 	max_mul = 100ull << (32 - hv_clock->tsc_shift);
908095cf55dSPaolo Bonzini 	if (hv_clock->tsc_to_system_mul >= max_mul)
909095cf55dSPaolo Bonzini 		return false;
910095cf55dSPaolo Bonzini 
911095cf55dSPaolo Bonzini 	/*
912095cf55dSPaolo Bonzini 	 * Otherwise compute the scale and offset according to the formulas
913095cf55dSPaolo Bonzini 	 * derived above.
914095cf55dSPaolo Bonzini 	 */
915095cf55dSPaolo Bonzini 	tsc_ref->tsc_scale =
916095cf55dSPaolo Bonzini 		mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift),
917095cf55dSPaolo Bonzini 				hv_clock->tsc_to_system_mul,
918095cf55dSPaolo Bonzini 				100);
919095cf55dSPaolo Bonzini 
920095cf55dSPaolo Bonzini 	tsc_ref->tsc_offset = hv_clock->system_time;
921095cf55dSPaolo Bonzini 	do_div(tsc_ref->tsc_offset, 100);
922095cf55dSPaolo Bonzini 	tsc_ref->tsc_offset -=
923095cf55dSPaolo Bonzini 		mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64);
924095cf55dSPaolo Bonzini 	return true;
925095cf55dSPaolo Bonzini }
926095cf55dSPaolo Bonzini 
927095cf55dSPaolo Bonzini void kvm_hv_setup_tsc_page(struct kvm *kvm,
928095cf55dSPaolo Bonzini 			   struct pvclock_vcpu_time_info *hv_clock)
929095cf55dSPaolo Bonzini {
930095cf55dSPaolo Bonzini 	struct kvm_hv *hv = &kvm->arch.hyperv;
931095cf55dSPaolo Bonzini 	u32 tsc_seq;
932095cf55dSPaolo Bonzini 	u64 gfn;
933095cf55dSPaolo Bonzini 
934095cf55dSPaolo Bonzini 	BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence));
935095cf55dSPaolo Bonzini 	BUILD_BUG_ON(offsetof(HV_REFERENCE_TSC_PAGE, tsc_sequence) != 0);
936095cf55dSPaolo Bonzini 
937095cf55dSPaolo Bonzini 	if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
938095cf55dSPaolo Bonzini 		return;
939095cf55dSPaolo Bonzini 
9403f5ad8beSPaolo Bonzini 	mutex_lock(&kvm->arch.hyperv.hv_lock);
9413f5ad8beSPaolo Bonzini 	if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
9423f5ad8beSPaolo Bonzini 		goto out_unlock;
9433f5ad8beSPaolo Bonzini 
944095cf55dSPaolo Bonzini 	gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
945095cf55dSPaolo Bonzini 	/*
946095cf55dSPaolo Bonzini 	 * Because the TSC parameters only vary when there is a
947095cf55dSPaolo Bonzini 	 * change in the master clock, do not bother with caching.
948095cf55dSPaolo Bonzini 	 */
949095cf55dSPaolo Bonzini 	if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn),
950095cf55dSPaolo Bonzini 				    &tsc_seq, sizeof(tsc_seq))))
9513f5ad8beSPaolo Bonzini 		goto out_unlock;
952095cf55dSPaolo Bonzini 
953095cf55dSPaolo Bonzini 	/*
954095cf55dSPaolo Bonzini 	 * While we're computing and writing the parameters, force the
955095cf55dSPaolo Bonzini 	 * guest to use the time reference count MSR.
956095cf55dSPaolo Bonzini 	 */
957095cf55dSPaolo Bonzini 	hv->tsc_ref.tsc_sequence = 0;
958095cf55dSPaolo Bonzini 	if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
959095cf55dSPaolo Bonzini 			    &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
9603f5ad8beSPaolo Bonzini 		goto out_unlock;
961095cf55dSPaolo Bonzini 
962095cf55dSPaolo Bonzini 	if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref))
9633f5ad8beSPaolo Bonzini 		goto out_unlock;
964095cf55dSPaolo Bonzini 
965095cf55dSPaolo Bonzini 	/* Ensure sequence is zero before writing the rest of the struct.  */
966095cf55dSPaolo Bonzini 	smp_wmb();
967095cf55dSPaolo Bonzini 	if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
9683f5ad8beSPaolo Bonzini 		goto out_unlock;
969095cf55dSPaolo Bonzini 
970095cf55dSPaolo Bonzini 	/*
971095cf55dSPaolo Bonzini 	 * Now switch to the TSC page mechanism by writing the sequence.
972095cf55dSPaolo Bonzini 	 */
973095cf55dSPaolo Bonzini 	tsc_seq++;
974095cf55dSPaolo Bonzini 	if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0)
975095cf55dSPaolo Bonzini 		tsc_seq = 1;
976095cf55dSPaolo Bonzini 
977095cf55dSPaolo Bonzini 	/* Write the struct entirely before the non-zero sequence.  */
978095cf55dSPaolo Bonzini 	smp_wmb();
979095cf55dSPaolo Bonzini 
980095cf55dSPaolo Bonzini 	hv->tsc_ref.tsc_sequence = tsc_seq;
981095cf55dSPaolo Bonzini 	kvm_write_guest(kvm, gfn_to_gpa(gfn),
982095cf55dSPaolo Bonzini 			&hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence));
9833f5ad8beSPaolo Bonzini out_unlock:
9843f5ad8beSPaolo Bonzini 	mutex_unlock(&kvm->arch.hyperv.hv_lock);
985095cf55dSPaolo Bonzini }
986095cf55dSPaolo Bonzini 
987e7d9513bSAndrey Smetanin static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
988e7d9513bSAndrey Smetanin 			     bool host)
989e83d5887SAndrey Smetanin {
990e83d5887SAndrey Smetanin 	struct kvm *kvm = vcpu->kvm;
991e83d5887SAndrey Smetanin 	struct kvm_hv *hv = &kvm->arch.hyperv;
992e83d5887SAndrey Smetanin 
993e83d5887SAndrey Smetanin 	switch (msr) {
994e83d5887SAndrey Smetanin 	case HV_X64_MSR_GUEST_OS_ID:
995e83d5887SAndrey Smetanin 		hv->hv_guest_os_id = data;
996e83d5887SAndrey Smetanin 		/* setting guest os id to zero disables hypercall page */
997e83d5887SAndrey Smetanin 		if (!hv->hv_guest_os_id)
998e83d5887SAndrey Smetanin 			hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
999e83d5887SAndrey Smetanin 		break;
1000e83d5887SAndrey Smetanin 	case HV_X64_MSR_HYPERCALL: {
1001e83d5887SAndrey Smetanin 		u64 gfn;
1002e83d5887SAndrey Smetanin 		unsigned long addr;
1003e83d5887SAndrey Smetanin 		u8 instructions[4];
1004e83d5887SAndrey Smetanin 
1005e83d5887SAndrey Smetanin 		/* if guest os id is not set hypercall should remain disabled */
1006e83d5887SAndrey Smetanin 		if (!hv->hv_guest_os_id)
1007e83d5887SAndrey Smetanin 			break;
1008e83d5887SAndrey Smetanin 		if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
1009e83d5887SAndrey Smetanin 			hv->hv_hypercall = data;
1010e83d5887SAndrey Smetanin 			break;
1011e83d5887SAndrey Smetanin 		}
1012e83d5887SAndrey Smetanin 		gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
1013e83d5887SAndrey Smetanin 		addr = gfn_to_hva(kvm, gfn);
1014e83d5887SAndrey Smetanin 		if (kvm_is_error_hva(addr))
1015e83d5887SAndrey Smetanin 			return 1;
1016e83d5887SAndrey Smetanin 		kvm_x86_ops->patch_hypercall(vcpu, instructions);
1017e83d5887SAndrey Smetanin 		((unsigned char *)instructions)[3] = 0xc3; /* ret */
1018e83d5887SAndrey Smetanin 		if (__copy_to_user((void __user *)addr, instructions, 4))
1019e83d5887SAndrey Smetanin 			return 1;
1020e83d5887SAndrey Smetanin 		hv->hv_hypercall = data;
1021e83d5887SAndrey Smetanin 		mark_page_dirty(kvm, gfn);
1022e83d5887SAndrey Smetanin 		break;
1023e83d5887SAndrey Smetanin 	}
1024095cf55dSPaolo Bonzini 	case HV_X64_MSR_REFERENCE_TSC:
1025e83d5887SAndrey Smetanin 		hv->hv_tsc_page = data;
1026095cf55dSPaolo Bonzini 		if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)
1027095cf55dSPaolo Bonzini 			kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1028e83d5887SAndrey Smetanin 		break;
1029e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1030e7d9513bSAndrey Smetanin 		return kvm_hv_msr_set_crash_data(vcpu,
1031e7d9513bSAndrey Smetanin 						 msr - HV_X64_MSR_CRASH_P0,
1032e7d9513bSAndrey Smetanin 						 data);
1033e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_CTL:
1034e7d9513bSAndrey Smetanin 		return kvm_hv_msr_set_crash_ctl(vcpu, data, host);
1035e516cebbSAndrey Smetanin 	case HV_X64_MSR_RESET:
1036e516cebbSAndrey Smetanin 		if (data == 1) {
1037e516cebbSAndrey Smetanin 			vcpu_debug(vcpu, "hyper-v reset requested\n");
1038e516cebbSAndrey Smetanin 			kvm_make_request(KVM_REQ_HV_RESET, vcpu);
1039e516cebbSAndrey Smetanin 		}
1040e516cebbSAndrey Smetanin 		break;
1041a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1042a2e164e7SVitaly Kuznetsov 		hv->hv_reenlightenment_control = data;
1043a2e164e7SVitaly Kuznetsov 		break;
1044a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_TSC_EMULATION_CONTROL:
1045a2e164e7SVitaly Kuznetsov 		hv->hv_tsc_emulation_control = data;
1046a2e164e7SVitaly Kuznetsov 		break;
1047a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_TSC_EMULATION_STATUS:
1048a2e164e7SVitaly Kuznetsov 		hv->hv_tsc_emulation_status = data;
1049a2e164e7SVitaly Kuznetsov 		break;
105044883f01SPaolo Bonzini 	case HV_X64_MSR_TIME_REF_COUNT:
105144883f01SPaolo Bonzini 		/* read-only, but still ignore it if host-initiated */
105244883f01SPaolo Bonzini 		if (!host)
105344883f01SPaolo Bonzini 			return 1;
105444883f01SPaolo Bonzini 		break;
1055e83d5887SAndrey Smetanin 	default:
1056e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
1057e83d5887SAndrey Smetanin 			    msr, data);
1058e83d5887SAndrey Smetanin 		return 1;
1059e83d5887SAndrey Smetanin 	}
1060e83d5887SAndrey Smetanin 	return 0;
1061e83d5887SAndrey Smetanin }
1062e83d5887SAndrey Smetanin 
10639eec50b8SAndrey Smetanin /* Calculate cpu time spent by current task in 100ns units */
10649eec50b8SAndrey Smetanin static u64 current_task_runtime_100ns(void)
10659eec50b8SAndrey Smetanin {
10665613fda9SFrederic Weisbecker 	u64 utime, stime;
10679eec50b8SAndrey Smetanin 
10689eec50b8SAndrey Smetanin 	task_cputime_adjusted(current, &utime, &stime);
10695613fda9SFrederic Weisbecker 
10705613fda9SFrederic Weisbecker 	return div_u64(utime + stime, 100);
10719eec50b8SAndrey Smetanin }
10729eec50b8SAndrey Smetanin 
10739eec50b8SAndrey Smetanin static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
1074e83d5887SAndrey Smetanin {
10751779a39fSVitaly Kuznetsov 	struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
1076e83d5887SAndrey Smetanin 
1077e83d5887SAndrey Smetanin 	switch (msr) {
107887ee613dSVitaly Kuznetsov 	case HV_X64_MSR_VP_INDEX: {
107987ee613dSVitaly Kuznetsov 		struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
108087ee613dSVitaly Kuznetsov 		int vcpu_idx = kvm_vcpu_get_idx(vcpu);
108187ee613dSVitaly Kuznetsov 		u32 new_vp_index = (u32)data;
108287ee613dSVitaly Kuznetsov 
108387ee613dSVitaly Kuznetsov 		if (!host || new_vp_index >= KVM_MAX_VCPUS)
1084d3457c87SRoman Kagan 			return 1;
108587ee613dSVitaly Kuznetsov 
108687ee613dSVitaly Kuznetsov 		if (new_vp_index == hv_vcpu->vp_index)
108787ee613dSVitaly Kuznetsov 			return 0;
108887ee613dSVitaly Kuznetsov 
108987ee613dSVitaly Kuznetsov 		/*
109087ee613dSVitaly Kuznetsov 		 * The VP index is initialized to vcpu_index by
109187ee613dSVitaly Kuznetsov 		 * kvm_hv_vcpu_postcreate so they initially match.  Now the
109287ee613dSVitaly Kuznetsov 		 * VP index is changing, adjust num_mismatched_vp_indexes if
109387ee613dSVitaly Kuznetsov 		 * it now matches or no longer matches vcpu_idx.
109487ee613dSVitaly Kuznetsov 		 */
109587ee613dSVitaly Kuznetsov 		if (hv_vcpu->vp_index == vcpu_idx)
109687ee613dSVitaly Kuznetsov 			atomic_inc(&hv->num_mismatched_vp_indexes);
109787ee613dSVitaly Kuznetsov 		else if (new_vp_index == vcpu_idx)
109887ee613dSVitaly Kuznetsov 			atomic_dec(&hv->num_mismatched_vp_indexes);
109987ee613dSVitaly Kuznetsov 
110087ee613dSVitaly Kuznetsov 		hv_vcpu->vp_index = new_vp_index;
1101d3457c87SRoman Kagan 		break;
110287ee613dSVitaly Kuznetsov 	}
1103d4abc577SLadi Prosek 	case HV_X64_MSR_VP_ASSIST_PAGE: {
1104e83d5887SAndrey Smetanin 		u64 gfn;
1105e83d5887SAndrey Smetanin 		unsigned long addr;
1106e83d5887SAndrey Smetanin 
1107d4abc577SLadi Prosek 		if (!(data & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE)) {
11081779a39fSVitaly Kuznetsov 			hv_vcpu->hv_vapic = data;
110972bbf935SLadi Prosek 			if (kvm_lapic_enable_pv_eoi(vcpu, 0, 0))
1110e83d5887SAndrey Smetanin 				return 1;
1111e83d5887SAndrey Smetanin 			break;
1112e83d5887SAndrey Smetanin 		}
1113d4abc577SLadi Prosek 		gfn = data >> HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT;
1114e83d5887SAndrey Smetanin 		addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
1115e83d5887SAndrey Smetanin 		if (kvm_is_error_hva(addr))
1116e83d5887SAndrey Smetanin 			return 1;
111712e0c618SVitaly Kuznetsov 
111812e0c618SVitaly Kuznetsov 		/*
111912e0c618SVitaly Kuznetsov 		 * Clear apic_assist portion of f(struct hv_vp_assist_page
112012e0c618SVitaly Kuznetsov 		 * only, there can be valuable data in the rest which needs
112112e0c618SVitaly Kuznetsov 		 * to be preserved e.g. on migration.
112212e0c618SVitaly Kuznetsov 		 */
112312e0c618SVitaly Kuznetsov 		if (__clear_user((void __user *)addr, sizeof(u32)))
1124e83d5887SAndrey Smetanin 			return 1;
11251779a39fSVitaly Kuznetsov 		hv_vcpu->hv_vapic = data;
1126e83d5887SAndrey Smetanin 		kvm_vcpu_mark_page_dirty(vcpu, gfn);
1127e83d5887SAndrey Smetanin 		if (kvm_lapic_enable_pv_eoi(vcpu,
112872bbf935SLadi Prosek 					    gfn_to_gpa(gfn) | KVM_MSR_ENABLED,
112972bbf935SLadi Prosek 					    sizeof(struct hv_vp_assist_page)))
1130e83d5887SAndrey Smetanin 			return 1;
1131e83d5887SAndrey Smetanin 		break;
1132e83d5887SAndrey Smetanin 	}
1133e83d5887SAndrey Smetanin 	case HV_X64_MSR_EOI:
1134e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1135e83d5887SAndrey Smetanin 	case HV_X64_MSR_ICR:
1136e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1137e83d5887SAndrey Smetanin 	case HV_X64_MSR_TPR:
1138e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
11399eec50b8SAndrey Smetanin 	case HV_X64_MSR_VP_RUNTIME:
11409eec50b8SAndrey Smetanin 		if (!host)
11419eec50b8SAndrey Smetanin 			return 1;
11421779a39fSVitaly Kuznetsov 		hv_vcpu->runtime_offset = data - current_task_runtime_100ns();
11439eec50b8SAndrey Smetanin 		break;
11445c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
11455c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
11465c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
11475c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
11485c919412SAndrey Smetanin 	case HV_X64_MSR_EOM:
11495c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
11505c919412SAndrey Smetanin 		return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host);
11511f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER0_CONFIG:
11521f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER1_CONFIG:
11531f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER2_CONFIG:
11541f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER3_CONFIG: {
11551f4b34f8SAndrey Smetanin 		int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
11561f4b34f8SAndrey Smetanin 
11571f4b34f8SAndrey Smetanin 		return stimer_set_config(vcpu_to_stimer(vcpu, timer_index),
11581f4b34f8SAndrey Smetanin 					 data, host);
11591f4b34f8SAndrey Smetanin 	}
11601f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER0_COUNT:
11611f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER1_COUNT:
11621f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER2_COUNT:
11631f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER3_COUNT: {
11641f4b34f8SAndrey Smetanin 		int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
11651f4b34f8SAndrey Smetanin 
11661f4b34f8SAndrey Smetanin 		return stimer_set_count(vcpu_to_stimer(vcpu, timer_index),
11671f4b34f8SAndrey Smetanin 					data, host);
11681f4b34f8SAndrey Smetanin 	}
116944883f01SPaolo Bonzini 	case HV_X64_MSR_TSC_FREQUENCY:
117044883f01SPaolo Bonzini 	case HV_X64_MSR_APIC_FREQUENCY:
117144883f01SPaolo Bonzini 		/* read-only, but still ignore it if host-initiated */
117244883f01SPaolo Bonzini 		if (!host)
117344883f01SPaolo Bonzini 			return 1;
117444883f01SPaolo Bonzini 		break;
1175e83d5887SAndrey Smetanin 	default:
1176e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
1177e83d5887SAndrey Smetanin 			    msr, data);
1178e83d5887SAndrey Smetanin 		return 1;
1179e83d5887SAndrey Smetanin 	}
1180e83d5887SAndrey Smetanin 
1181e83d5887SAndrey Smetanin 	return 0;
1182e83d5887SAndrey Smetanin }
1183e83d5887SAndrey Smetanin 
1184e83d5887SAndrey Smetanin static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1185e83d5887SAndrey Smetanin {
1186e83d5887SAndrey Smetanin 	u64 data = 0;
1187e83d5887SAndrey Smetanin 	struct kvm *kvm = vcpu->kvm;
1188e83d5887SAndrey Smetanin 	struct kvm_hv *hv = &kvm->arch.hyperv;
1189e83d5887SAndrey Smetanin 
1190e83d5887SAndrey Smetanin 	switch (msr) {
1191e83d5887SAndrey Smetanin 	case HV_X64_MSR_GUEST_OS_ID:
1192e83d5887SAndrey Smetanin 		data = hv->hv_guest_os_id;
1193e83d5887SAndrey Smetanin 		break;
1194e83d5887SAndrey Smetanin 	case HV_X64_MSR_HYPERCALL:
1195e83d5887SAndrey Smetanin 		data = hv->hv_hypercall;
1196e83d5887SAndrey Smetanin 		break;
119793bf4172SAndrey Smetanin 	case HV_X64_MSR_TIME_REF_COUNT:
119893bf4172SAndrey Smetanin 		data = get_time_ref_counter(kvm);
1199e83d5887SAndrey Smetanin 		break;
1200e83d5887SAndrey Smetanin 	case HV_X64_MSR_REFERENCE_TSC:
1201e83d5887SAndrey Smetanin 		data = hv->hv_tsc_page;
1202e83d5887SAndrey Smetanin 		break;
1203e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1204e7d9513bSAndrey Smetanin 		return kvm_hv_msr_get_crash_data(vcpu,
1205e7d9513bSAndrey Smetanin 						 msr - HV_X64_MSR_CRASH_P0,
1206e7d9513bSAndrey Smetanin 						 pdata);
1207e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_CTL:
1208e7d9513bSAndrey Smetanin 		return kvm_hv_msr_get_crash_ctl(vcpu, pdata);
1209e516cebbSAndrey Smetanin 	case HV_X64_MSR_RESET:
1210e516cebbSAndrey Smetanin 		data = 0;
1211e516cebbSAndrey Smetanin 		break;
1212a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1213a2e164e7SVitaly Kuznetsov 		data = hv->hv_reenlightenment_control;
1214a2e164e7SVitaly Kuznetsov 		break;
1215a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_TSC_EMULATION_CONTROL:
1216a2e164e7SVitaly Kuznetsov 		data = hv->hv_tsc_emulation_control;
1217a2e164e7SVitaly Kuznetsov 		break;
1218a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_TSC_EMULATION_STATUS:
1219a2e164e7SVitaly Kuznetsov 		data = hv->hv_tsc_emulation_status;
1220a2e164e7SVitaly Kuznetsov 		break;
1221e83d5887SAndrey Smetanin 	default:
1222e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1223e83d5887SAndrey Smetanin 		return 1;
1224e83d5887SAndrey Smetanin 	}
1225e83d5887SAndrey Smetanin 
1226e83d5887SAndrey Smetanin 	*pdata = data;
1227e83d5887SAndrey Smetanin 	return 0;
1228e83d5887SAndrey Smetanin }
1229e83d5887SAndrey Smetanin 
123044883f01SPaolo Bonzini static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,
123144883f01SPaolo Bonzini 			  bool host)
1232e83d5887SAndrey Smetanin {
1233e83d5887SAndrey Smetanin 	u64 data = 0;
12341779a39fSVitaly Kuznetsov 	struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
1235e83d5887SAndrey Smetanin 
1236e83d5887SAndrey Smetanin 	switch (msr) {
1237d3457c87SRoman Kagan 	case HV_X64_MSR_VP_INDEX:
12381779a39fSVitaly Kuznetsov 		data = hv_vcpu->vp_index;
1239e83d5887SAndrey Smetanin 		break;
1240e83d5887SAndrey Smetanin 	case HV_X64_MSR_EOI:
1241e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1242e83d5887SAndrey Smetanin 	case HV_X64_MSR_ICR:
1243e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1244e83d5887SAndrey Smetanin 	case HV_X64_MSR_TPR:
1245e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
1246d4abc577SLadi Prosek 	case HV_X64_MSR_VP_ASSIST_PAGE:
12471779a39fSVitaly Kuznetsov 		data = hv_vcpu->hv_vapic;
1248e83d5887SAndrey Smetanin 		break;
12499eec50b8SAndrey Smetanin 	case HV_X64_MSR_VP_RUNTIME:
12501779a39fSVitaly Kuznetsov 		data = current_task_runtime_100ns() + hv_vcpu->runtime_offset;
12519eec50b8SAndrey Smetanin 		break;
12525c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
12535c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
12545c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
12555c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
12565c919412SAndrey Smetanin 	case HV_X64_MSR_EOM:
12575c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
125844883f01SPaolo Bonzini 		return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata, host);
12591f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER0_CONFIG:
12601f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER1_CONFIG:
12611f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER2_CONFIG:
12621f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER3_CONFIG: {
12631f4b34f8SAndrey Smetanin 		int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
12641f4b34f8SAndrey Smetanin 
12651f4b34f8SAndrey Smetanin 		return stimer_get_config(vcpu_to_stimer(vcpu, timer_index),
12661f4b34f8SAndrey Smetanin 					 pdata);
12671f4b34f8SAndrey Smetanin 	}
12681f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER0_COUNT:
12691f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER1_COUNT:
12701f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER2_COUNT:
12711f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER3_COUNT: {
12721f4b34f8SAndrey Smetanin 		int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
12731f4b34f8SAndrey Smetanin 
12741f4b34f8SAndrey Smetanin 		return stimer_get_count(vcpu_to_stimer(vcpu, timer_index),
12751f4b34f8SAndrey Smetanin 					pdata);
12761f4b34f8SAndrey Smetanin 	}
127772c139baSLadi Prosek 	case HV_X64_MSR_TSC_FREQUENCY:
127872c139baSLadi Prosek 		data = (u64)vcpu->arch.virtual_tsc_khz * 1000;
127972c139baSLadi Prosek 		break;
128072c139baSLadi Prosek 	case HV_X64_MSR_APIC_FREQUENCY:
128172c139baSLadi Prosek 		data = APIC_BUS_FREQUENCY;
128272c139baSLadi Prosek 		break;
1283e83d5887SAndrey Smetanin 	default:
1284e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1285e83d5887SAndrey Smetanin 		return 1;
1286e83d5887SAndrey Smetanin 	}
1287e83d5887SAndrey Smetanin 	*pdata = data;
1288e83d5887SAndrey Smetanin 	return 0;
1289e83d5887SAndrey Smetanin }
1290e83d5887SAndrey Smetanin 
1291e7d9513bSAndrey Smetanin int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
1292e83d5887SAndrey Smetanin {
1293e83d5887SAndrey Smetanin 	if (kvm_hv_msr_partition_wide(msr)) {
1294e83d5887SAndrey Smetanin 		int r;
1295e83d5887SAndrey Smetanin 
12963f5ad8beSPaolo Bonzini 		mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
1297e7d9513bSAndrey Smetanin 		r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
12983f5ad8beSPaolo Bonzini 		mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
1299e83d5887SAndrey Smetanin 		return r;
1300e83d5887SAndrey Smetanin 	} else
13019eec50b8SAndrey Smetanin 		return kvm_hv_set_msr(vcpu, msr, data, host);
1302e83d5887SAndrey Smetanin }
1303e83d5887SAndrey Smetanin 
130444883f01SPaolo Bonzini int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
1305e83d5887SAndrey Smetanin {
1306e83d5887SAndrey Smetanin 	if (kvm_hv_msr_partition_wide(msr)) {
1307e83d5887SAndrey Smetanin 		int r;
1308e83d5887SAndrey Smetanin 
13093f5ad8beSPaolo Bonzini 		mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
1310e83d5887SAndrey Smetanin 		r = kvm_hv_get_msr_pw(vcpu, msr, pdata);
13113f5ad8beSPaolo Bonzini 		mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
1312e83d5887SAndrey Smetanin 		return r;
1313e83d5887SAndrey Smetanin 	} else
131444883f01SPaolo Bonzini 		return kvm_hv_get_msr(vcpu, msr, pdata, host);
1315e83d5887SAndrey Smetanin }
1316e83d5887SAndrey Smetanin 
1317f21dd494SVitaly Kuznetsov static __always_inline unsigned long *sparse_set_to_vcpu_mask(
1318f21dd494SVitaly Kuznetsov 	struct kvm *kvm, u64 *sparse_banks, u64 valid_bank_mask,
1319f21dd494SVitaly Kuznetsov 	u64 *vp_bitmap, unsigned long *vcpu_bitmap)
1320c7012676SVitaly Kuznetsov {
1321f21dd494SVitaly Kuznetsov 	struct kvm_hv *hv = &kvm->arch.hyperv;
1322f21dd494SVitaly Kuznetsov 	struct kvm_vcpu *vcpu;
1323f21dd494SVitaly Kuznetsov 	int i, bank, sbank = 0;
1324c7012676SVitaly Kuznetsov 
1325f21dd494SVitaly Kuznetsov 	memset(vp_bitmap, 0,
1326f21dd494SVitaly Kuznetsov 	       KVM_HV_MAX_SPARSE_VCPU_SET_BITS * sizeof(*vp_bitmap));
1327f21dd494SVitaly Kuznetsov 	for_each_set_bit(bank, (unsigned long *)&valid_bank_mask,
1328f21dd494SVitaly Kuznetsov 			 KVM_HV_MAX_SPARSE_VCPU_SET_BITS)
1329f21dd494SVitaly Kuznetsov 		vp_bitmap[bank] = sparse_banks[sbank++];
1330c7012676SVitaly Kuznetsov 
1331f21dd494SVitaly Kuznetsov 	if (likely(!atomic_read(&hv->num_mismatched_vp_indexes))) {
1332f21dd494SVitaly Kuznetsov 		/* for all vcpus vp_index == vcpu_idx */
1333f21dd494SVitaly Kuznetsov 		return (unsigned long *)vp_bitmap;
1334f21dd494SVitaly Kuznetsov 	}
1335c7012676SVitaly Kuznetsov 
1336f21dd494SVitaly Kuznetsov 	bitmap_zero(vcpu_bitmap, KVM_MAX_VCPUS);
1337f21dd494SVitaly Kuznetsov 	kvm_for_each_vcpu(i, vcpu, kvm) {
1338f21dd494SVitaly Kuznetsov 		if (test_bit(vcpu_to_hv_vcpu(vcpu)->vp_index,
1339f21dd494SVitaly Kuznetsov 			     (unsigned long *)vp_bitmap))
1340f21dd494SVitaly Kuznetsov 			__set_bit(i, vcpu_bitmap);
1341f21dd494SVitaly Kuznetsov 	}
1342f21dd494SVitaly Kuznetsov 	return vcpu_bitmap;
1343c7012676SVitaly Kuznetsov }
1344c7012676SVitaly Kuznetsov 
1345e2f11f42SVitaly Kuznetsov static u64 kvm_hv_flush_tlb(struct kvm_vcpu *current_vcpu, u64 ingpa,
1346c7012676SVitaly Kuznetsov 			    u16 rep_cnt, bool ex)
1347e2f11f42SVitaly Kuznetsov {
1348e2f11f42SVitaly Kuznetsov 	struct kvm *kvm = current_vcpu->kvm;
13492cefc5feSVitaly Kuznetsov 	struct kvm_vcpu_hv *hv_vcpu = &current_vcpu->arch.hyperv;
1350c7012676SVitaly Kuznetsov 	struct hv_tlb_flush_ex flush_ex;
1351e2f11f42SVitaly Kuznetsov 	struct hv_tlb_flush flush;
1352f21dd494SVitaly Kuznetsov 	u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS];
1353f21dd494SVitaly Kuznetsov 	DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS);
1354f21dd494SVitaly Kuznetsov 	unsigned long *vcpu_mask;
13552cefc5feSVitaly Kuznetsov 	u64 valid_bank_mask;
1356c7012676SVitaly Kuznetsov 	u64 sparse_banks[64];
1357f21dd494SVitaly Kuznetsov 	int sparse_banks_len;
1358c7012676SVitaly Kuznetsov 	bool all_cpus;
1359e2f11f42SVitaly Kuznetsov 
1360c7012676SVitaly Kuznetsov 	if (!ex) {
1361e2f11f42SVitaly Kuznetsov 		if (unlikely(kvm_read_guest(kvm, ingpa, &flush, sizeof(flush))))
1362e2f11f42SVitaly Kuznetsov 			return HV_STATUS_INVALID_HYPERCALL_INPUT;
1363e2f11f42SVitaly Kuznetsov 
1364c7012676SVitaly Kuznetsov 		trace_kvm_hv_flush_tlb(flush.processor_mask,
1365c7012676SVitaly Kuznetsov 				       flush.address_space, flush.flags);
1366c7012676SVitaly Kuznetsov 
13672cefc5feSVitaly Kuznetsov 		valid_bank_mask = BIT_ULL(0);
1368c7012676SVitaly Kuznetsov 		sparse_banks[0] = flush.processor_mask;
1369c7012676SVitaly Kuznetsov 		all_cpus = flush.flags & HV_FLUSH_ALL_PROCESSORS;
1370c7012676SVitaly Kuznetsov 	} else {
1371c7012676SVitaly Kuznetsov 		if (unlikely(kvm_read_guest(kvm, ingpa, &flush_ex,
1372c7012676SVitaly Kuznetsov 					    sizeof(flush_ex))))
1373c7012676SVitaly Kuznetsov 			return HV_STATUS_INVALID_HYPERCALL_INPUT;
1374c7012676SVitaly Kuznetsov 
1375c7012676SVitaly Kuznetsov 		trace_kvm_hv_flush_tlb_ex(flush_ex.hv_vp_set.valid_bank_mask,
1376c7012676SVitaly Kuznetsov 					  flush_ex.hv_vp_set.format,
1377c7012676SVitaly Kuznetsov 					  flush_ex.address_space,
1378c7012676SVitaly Kuznetsov 					  flush_ex.flags);
1379c7012676SVitaly Kuznetsov 
1380c7012676SVitaly Kuznetsov 		valid_bank_mask = flush_ex.hv_vp_set.valid_bank_mask;
1381c7012676SVitaly Kuznetsov 		all_cpus = flush_ex.hv_vp_set.format !=
1382c7012676SVitaly Kuznetsov 			HV_GENERIC_SET_SPARSE_4K;
1383c7012676SVitaly Kuznetsov 
13840b0a31baSVitaly Kuznetsov 		sparse_banks_len =
13850b0a31baSVitaly Kuznetsov 			bitmap_weight((unsigned long *)&valid_bank_mask, 64) *
1386c7012676SVitaly Kuznetsov 			sizeof(sparse_banks[0]);
1387c7012676SVitaly Kuznetsov 
1388c7012676SVitaly Kuznetsov 		if (!sparse_banks_len && !all_cpus)
1389c7012676SVitaly Kuznetsov 			goto ret_success;
1390c7012676SVitaly Kuznetsov 
1391c7012676SVitaly Kuznetsov 		if (!all_cpus &&
1392c7012676SVitaly Kuznetsov 		    kvm_read_guest(kvm,
1393c7012676SVitaly Kuznetsov 				   ingpa + offsetof(struct hv_tlb_flush_ex,
1394c7012676SVitaly Kuznetsov 						    hv_vp_set.bank_contents),
1395c7012676SVitaly Kuznetsov 				   sparse_banks,
1396c7012676SVitaly Kuznetsov 				   sparse_banks_len))
1397c7012676SVitaly Kuznetsov 			return HV_STATUS_INVALID_HYPERCALL_INPUT;
1398c7012676SVitaly Kuznetsov 	}
1399e2f11f42SVitaly Kuznetsov 
1400f21dd494SVitaly Kuznetsov 	cpumask_clear(&hv_vcpu->tlb_flush);
1401f21dd494SVitaly Kuznetsov 
1402f21dd494SVitaly Kuznetsov 	vcpu_mask = all_cpus ? NULL :
1403f21dd494SVitaly Kuznetsov 		sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask,
1404f21dd494SVitaly Kuznetsov 					vp_bitmap, vcpu_bitmap);
1405f21dd494SVitaly Kuznetsov 
14062cefc5feSVitaly Kuznetsov 	/*
14072cefc5feSVitaly Kuznetsov 	 * vcpu->arch.cr3 may not be up-to-date for running vCPUs so we can't
14082cefc5feSVitaly Kuznetsov 	 * analyze it here, flush TLB regardless of the specified address space.
14092cefc5feSVitaly Kuznetsov 	 */
1410a812297cSVitaly Kuznetsov 	kvm_make_vcpus_request_mask(kvm,
1411a812297cSVitaly Kuznetsov 				    KVM_REQ_TLB_FLUSH | KVM_REQUEST_NO_WAKEUP,
1412f21dd494SVitaly Kuznetsov 				    vcpu_mask, &hv_vcpu->tlb_flush);
1413e2f11f42SVitaly Kuznetsov 
1414c7012676SVitaly Kuznetsov ret_success:
1415e2f11f42SVitaly Kuznetsov 	/* We always do full TLB flush, set rep_done = rep_cnt. */
1416e2f11f42SVitaly Kuznetsov 	return (u64)HV_STATUS_SUCCESS |
1417e2f11f42SVitaly Kuznetsov 		((u64)rep_cnt << HV_HYPERCALL_REP_COMP_OFFSET);
1418e2f11f42SVitaly Kuznetsov }
1419e2f11f42SVitaly Kuznetsov 
1420f21dd494SVitaly Kuznetsov static void kvm_send_ipi_to_many(struct kvm *kvm, u32 vector,
1421f21dd494SVitaly Kuznetsov 				 unsigned long *vcpu_bitmap)
1422f21dd494SVitaly Kuznetsov {
1423f21dd494SVitaly Kuznetsov 	struct kvm_lapic_irq irq = {
1424f21dd494SVitaly Kuznetsov 		.delivery_mode = APIC_DM_FIXED,
1425f21dd494SVitaly Kuznetsov 		.vector = vector
1426f21dd494SVitaly Kuznetsov 	};
1427f21dd494SVitaly Kuznetsov 	struct kvm_vcpu *vcpu;
1428f21dd494SVitaly Kuznetsov 	int i;
1429f21dd494SVitaly Kuznetsov 
1430f21dd494SVitaly Kuznetsov 	kvm_for_each_vcpu(i, vcpu, kvm) {
1431f21dd494SVitaly Kuznetsov 		if (vcpu_bitmap && !test_bit(i, vcpu_bitmap))
1432f21dd494SVitaly Kuznetsov 			continue;
1433f21dd494SVitaly Kuznetsov 
1434f21dd494SVitaly Kuznetsov 		/* We fail only when APIC is disabled */
1435f21dd494SVitaly Kuznetsov 		kvm_apic_set_irq(vcpu, &irq, NULL);
1436f21dd494SVitaly Kuznetsov 	}
1437f21dd494SVitaly Kuznetsov }
1438f21dd494SVitaly Kuznetsov 
1439214ff83dSVitaly Kuznetsov static u64 kvm_hv_send_ipi(struct kvm_vcpu *current_vcpu, u64 ingpa, u64 outgpa,
1440214ff83dSVitaly Kuznetsov 			   bool ex, bool fast)
1441214ff83dSVitaly Kuznetsov {
1442214ff83dSVitaly Kuznetsov 	struct kvm *kvm = current_vcpu->kvm;
1443214ff83dSVitaly Kuznetsov 	struct hv_send_ipi_ex send_ipi_ex;
1444214ff83dSVitaly Kuznetsov 	struct hv_send_ipi send_ipi;
1445f21dd494SVitaly Kuznetsov 	u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS];
1446f21dd494SVitaly Kuznetsov 	DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS);
1447f21dd494SVitaly Kuznetsov 	unsigned long *vcpu_mask;
1448214ff83dSVitaly Kuznetsov 	unsigned long valid_bank_mask;
1449214ff83dSVitaly Kuznetsov 	u64 sparse_banks[64];
1450f21dd494SVitaly Kuznetsov 	int sparse_banks_len;
1451f21dd494SVitaly Kuznetsov 	u32 vector;
1452214ff83dSVitaly Kuznetsov 	bool all_cpus;
1453214ff83dSVitaly Kuznetsov 
1454214ff83dSVitaly Kuznetsov 	if (!ex) {
1455214ff83dSVitaly Kuznetsov 		if (!fast) {
1456214ff83dSVitaly Kuznetsov 			if (unlikely(kvm_read_guest(kvm, ingpa, &send_ipi,
1457214ff83dSVitaly Kuznetsov 						    sizeof(send_ipi))))
1458214ff83dSVitaly Kuznetsov 				return HV_STATUS_INVALID_HYPERCALL_INPUT;
1459214ff83dSVitaly Kuznetsov 			sparse_banks[0] = send_ipi.cpu_mask;
1460f21dd494SVitaly Kuznetsov 			vector = send_ipi.vector;
1461214ff83dSVitaly Kuznetsov 		} else {
1462214ff83dSVitaly Kuznetsov 			/* 'reserved' part of hv_send_ipi should be 0 */
1463214ff83dSVitaly Kuznetsov 			if (unlikely(ingpa >> 32 != 0))
1464214ff83dSVitaly Kuznetsov 				return HV_STATUS_INVALID_HYPERCALL_INPUT;
1465214ff83dSVitaly Kuznetsov 			sparse_banks[0] = outgpa;
1466f21dd494SVitaly Kuznetsov 			vector = (u32)ingpa;
1467214ff83dSVitaly Kuznetsov 		}
1468214ff83dSVitaly Kuznetsov 		all_cpus = false;
1469214ff83dSVitaly Kuznetsov 		valid_bank_mask = BIT_ULL(0);
1470214ff83dSVitaly Kuznetsov 
1471f21dd494SVitaly Kuznetsov 		trace_kvm_hv_send_ipi(vector, sparse_banks[0]);
1472214ff83dSVitaly Kuznetsov 	} else {
1473214ff83dSVitaly Kuznetsov 		if (unlikely(kvm_read_guest(kvm, ingpa, &send_ipi_ex,
1474214ff83dSVitaly Kuznetsov 					    sizeof(send_ipi_ex))))
1475214ff83dSVitaly Kuznetsov 			return HV_STATUS_INVALID_HYPERCALL_INPUT;
1476214ff83dSVitaly Kuznetsov 
1477214ff83dSVitaly Kuznetsov 		trace_kvm_hv_send_ipi_ex(send_ipi_ex.vector,
1478214ff83dSVitaly Kuznetsov 					 send_ipi_ex.vp_set.format,
1479214ff83dSVitaly Kuznetsov 					 send_ipi_ex.vp_set.valid_bank_mask);
1480214ff83dSVitaly Kuznetsov 
1481f21dd494SVitaly Kuznetsov 		vector = send_ipi_ex.vector;
1482214ff83dSVitaly Kuznetsov 		valid_bank_mask = send_ipi_ex.vp_set.valid_bank_mask;
1483214ff83dSVitaly Kuznetsov 		sparse_banks_len = bitmap_weight(&valid_bank_mask, 64) *
1484214ff83dSVitaly Kuznetsov 			sizeof(sparse_banks[0]);
1485214ff83dSVitaly Kuznetsov 
1486214ff83dSVitaly Kuznetsov 		all_cpus = send_ipi_ex.vp_set.format == HV_GENERIC_SET_ALL;
1487214ff83dSVitaly Kuznetsov 
1488214ff83dSVitaly Kuznetsov 		if (!sparse_banks_len)
1489214ff83dSVitaly Kuznetsov 			goto ret_success;
1490214ff83dSVitaly Kuznetsov 
1491214ff83dSVitaly Kuznetsov 		if (!all_cpus &&
1492214ff83dSVitaly Kuznetsov 		    kvm_read_guest(kvm,
1493214ff83dSVitaly Kuznetsov 				   ingpa + offsetof(struct hv_send_ipi_ex,
1494214ff83dSVitaly Kuznetsov 						    vp_set.bank_contents),
1495214ff83dSVitaly Kuznetsov 				   sparse_banks,
1496214ff83dSVitaly Kuznetsov 				   sparse_banks_len))
1497214ff83dSVitaly Kuznetsov 			return HV_STATUS_INVALID_HYPERCALL_INPUT;
1498214ff83dSVitaly Kuznetsov 	}
1499214ff83dSVitaly Kuznetsov 
1500f21dd494SVitaly Kuznetsov 	if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR))
1501214ff83dSVitaly Kuznetsov 		return HV_STATUS_INVALID_HYPERCALL_INPUT;
1502214ff83dSVitaly Kuznetsov 
1503f21dd494SVitaly Kuznetsov 	vcpu_mask = all_cpus ? NULL :
1504f21dd494SVitaly Kuznetsov 		sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask,
1505f21dd494SVitaly Kuznetsov 					vp_bitmap, vcpu_bitmap);
1506214ff83dSVitaly Kuznetsov 
1507f21dd494SVitaly Kuznetsov 	kvm_send_ipi_to_many(kvm, vector, vcpu_mask);
1508214ff83dSVitaly Kuznetsov 
1509214ff83dSVitaly Kuznetsov ret_success:
1510214ff83dSVitaly Kuznetsov 	return HV_STATUS_SUCCESS;
1511214ff83dSVitaly Kuznetsov }
1512214ff83dSVitaly Kuznetsov 
1513e83d5887SAndrey Smetanin bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1514e83d5887SAndrey Smetanin {
15153f5ad8beSPaolo Bonzini 	return READ_ONCE(kvm->arch.hyperv.hv_hypercall) & HV_X64_MSR_HYPERCALL_ENABLE;
1516e83d5887SAndrey Smetanin }
1517e83d5887SAndrey Smetanin 
151883326e43SAndrey Smetanin static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
151983326e43SAndrey Smetanin {
152083326e43SAndrey Smetanin 	bool longmode;
152183326e43SAndrey Smetanin 
152283326e43SAndrey Smetanin 	longmode = is_64_bit_mode(vcpu);
152383326e43SAndrey Smetanin 	if (longmode)
152483326e43SAndrey Smetanin 		kvm_register_write(vcpu, VCPU_REGS_RAX, result);
152583326e43SAndrey Smetanin 	else {
152683326e43SAndrey Smetanin 		kvm_register_write(vcpu, VCPU_REGS_RDX, result >> 32);
152783326e43SAndrey Smetanin 		kvm_register_write(vcpu, VCPU_REGS_RAX, result & 0xffffffff);
152883326e43SAndrey Smetanin 	}
152983326e43SAndrey Smetanin }
153083326e43SAndrey Smetanin 
1531696ca779SRadim Krčmář static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result)
1532696ca779SRadim Krčmář {
1533696ca779SRadim Krčmář 	kvm_hv_hypercall_set_result(vcpu, result);
1534696ca779SRadim Krčmář 	++vcpu->stat.hypercalls;
1535696ca779SRadim Krčmář 	return kvm_skip_emulated_instruction(vcpu);
1536696ca779SRadim Krčmář }
1537696ca779SRadim Krčmář 
153883326e43SAndrey Smetanin static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
153983326e43SAndrey Smetanin {
1540696ca779SRadim Krčmář 	return kvm_hv_hypercall_complete(vcpu, vcpu->run->hyperv.u.hcall.result);
154183326e43SAndrey Smetanin }
154283326e43SAndrey Smetanin 
1543faeb7833SRoman Kagan static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, bool fast, u64 param)
1544faeb7833SRoman Kagan {
1545faeb7833SRoman Kagan 	struct eventfd_ctx *eventfd;
1546faeb7833SRoman Kagan 
1547faeb7833SRoman Kagan 	if (unlikely(!fast)) {
1548faeb7833SRoman Kagan 		int ret;
1549faeb7833SRoman Kagan 		gpa_t gpa = param;
1550faeb7833SRoman Kagan 
1551faeb7833SRoman Kagan 		if ((gpa & (__alignof__(param) - 1)) ||
1552faeb7833SRoman Kagan 		    offset_in_page(gpa) + sizeof(param) > PAGE_SIZE)
1553faeb7833SRoman Kagan 			return HV_STATUS_INVALID_ALIGNMENT;
1554faeb7833SRoman Kagan 
1555faeb7833SRoman Kagan 		ret = kvm_vcpu_read_guest(vcpu, gpa, &param, sizeof(param));
1556faeb7833SRoman Kagan 		if (ret < 0)
1557faeb7833SRoman Kagan 			return HV_STATUS_INVALID_ALIGNMENT;
1558faeb7833SRoman Kagan 	}
1559faeb7833SRoman Kagan 
1560faeb7833SRoman Kagan 	/*
1561faeb7833SRoman Kagan 	 * Per spec, bits 32-47 contain the extra "flag number".  However, we
1562faeb7833SRoman Kagan 	 * have no use for it, and in all known usecases it is zero, so just
1563faeb7833SRoman Kagan 	 * report lookup failure if it isn't.
1564faeb7833SRoman Kagan 	 */
1565faeb7833SRoman Kagan 	if (param & 0xffff00000000ULL)
1566faeb7833SRoman Kagan 		return HV_STATUS_INVALID_PORT_ID;
1567faeb7833SRoman Kagan 	/* remaining bits are reserved-zero */
1568faeb7833SRoman Kagan 	if (param & ~KVM_HYPERV_CONN_ID_MASK)
1569faeb7833SRoman Kagan 		return HV_STATUS_INVALID_HYPERCALL_INPUT;
1570faeb7833SRoman Kagan 
1571452a68d0SPaolo Bonzini 	/* the eventfd is protected by vcpu->kvm->srcu, but conn_to_evt isn't */
1572452a68d0SPaolo Bonzini 	rcu_read_lock();
1573faeb7833SRoman Kagan 	eventfd = idr_find(&vcpu->kvm->arch.hyperv.conn_to_evt, param);
1574452a68d0SPaolo Bonzini 	rcu_read_unlock();
1575faeb7833SRoman Kagan 	if (!eventfd)
1576faeb7833SRoman Kagan 		return HV_STATUS_INVALID_PORT_ID;
1577faeb7833SRoman Kagan 
1578faeb7833SRoman Kagan 	eventfd_signal(eventfd, 1);
1579faeb7833SRoman Kagan 	return HV_STATUS_SUCCESS;
1580faeb7833SRoman Kagan }
1581faeb7833SRoman Kagan 
1582e83d5887SAndrey Smetanin int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
1583e83d5887SAndrey Smetanin {
1584d32ef547SDan Carpenter 	u64 param, ingpa, outgpa, ret = HV_STATUS_SUCCESS;
1585d32ef547SDan Carpenter 	uint16_t code, rep_idx, rep_cnt;
158656b9ae78SVitaly Kuznetsov 	bool fast, longmode, rep;
1587e83d5887SAndrey Smetanin 
1588e83d5887SAndrey Smetanin 	/*
1589e83d5887SAndrey Smetanin 	 * hypercall generates UD from non zero cpl and real mode
1590e83d5887SAndrey Smetanin 	 * per HYPER-V spec
1591e83d5887SAndrey Smetanin 	 */
1592e83d5887SAndrey Smetanin 	if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
1593e83d5887SAndrey Smetanin 		kvm_queue_exception(vcpu, UD_VECTOR);
15940d9c055eSAndrey Smetanin 		return 1;
1595e83d5887SAndrey Smetanin 	}
1596e83d5887SAndrey Smetanin 
1597e83d5887SAndrey Smetanin 	longmode = is_64_bit_mode(vcpu);
1598e83d5887SAndrey Smetanin 
1599e83d5887SAndrey Smetanin 	if (!longmode) {
1600e83d5887SAndrey Smetanin 		param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
1601e83d5887SAndrey Smetanin 			(kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
1602e83d5887SAndrey Smetanin 		ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
1603e83d5887SAndrey Smetanin 			(kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
1604e83d5887SAndrey Smetanin 		outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
1605e83d5887SAndrey Smetanin 			(kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
1606e83d5887SAndrey Smetanin 	}
1607e83d5887SAndrey Smetanin #ifdef CONFIG_X86_64
1608e83d5887SAndrey Smetanin 	else {
1609e83d5887SAndrey Smetanin 		param = kvm_register_read(vcpu, VCPU_REGS_RCX);
1610e83d5887SAndrey Smetanin 		ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
1611e83d5887SAndrey Smetanin 		outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
1612e83d5887SAndrey Smetanin 	}
1613e83d5887SAndrey Smetanin #endif
1614e83d5887SAndrey Smetanin 
1615e83d5887SAndrey Smetanin 	code = param & 0xffff;
1616142c95daSVitaly Kuznetsov 	fast = !!(param & HV_HYPERCALL_FAST_BIT);
1617142c95daSVitaly Kuznetsov 	rep_cnt = (param >> HV_HYPERCALL_REP_COMP_OFFSET) & 0xfff;
1618142c95daSVitaly Kuznetsov 	rep_idx = (param >> HV_HYPERCALL_REP_START_OFFSET) & 0xfff;
161956b9ae78SVitaly Kuznetsov 	rep = !!(rep_cnt || rep_idx);
1620e83d5887SAndrey Smetanin 
1621e83d5887SAndrey Smetanin 	trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
1622e83d5887SAndrey Smetanin 
1623e83d5887SAndrey Smetanin 	switch (code) {
16248ed6d767SAndrey Smetanin 	case HVCALL_NOTIFY_LONG_SPIN_WAIT:
162556b9ae78SVitaly Kuznetsov 		if (unlikely(rep)) {
162656b9ae78SVitaly Kuznetsov 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
162756b9ae78SVitaly Kuznetsov 			break;
162856b9ae78SVitaly Kuznetsov 		}
1629de63ad4cSLongpeng(Mike) 		kvm_vcpu_on_spin(vcpu, true);
1630e83d5887SAndrey Smetanin 		break;
163183326e43SAndrey Smetanin 	case HVCALL_SIGNAL_EVENT:
163256b9ae78SVitaly Kuznetsov 		if (unlikely(rep)) {
163356b9ae78SVitaly Kuznetsov 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
163456b9ae78SVitaly Kuznetsov 			break;
163556b9ae78SVitaly Kuznetsov 		}
1636d32ef547SDan Carpenter 		ret = kvm_hvcall_signal_event(vcpu, fast, ingpa);
1637d32ef547SDan Carpenter 		if (ret != HV_STATUS_INVALID_PORT_ID)
1638faeb7833SRoman Kagan 			break;
1639b2869f28SGustavo A. R. Silva 		/* fall through - maybe userspace knows this conn_id. */
1640faeb7833SRoman Kagan 	case HVCALL_POST_MESSAGE:
1641a2b5c3c0SPaolo Bonzini 		/* don't bother userspace if it has no way to handle it */
164256b9ae78SVitaly Kuznetsov 		if (unlikely(rep || !vcpu_to_synic(vcpu)->active)) {
164356b9ae78SVitaly Kuznetsov 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1644a2b5c3c0SPaolo Bonzini 			break;
1645a2b5c3c0SPaolo Bonzini 		}
164683326e43SAndrey Smetanin 		vcpu->run->exit_reason = KVM_EXIT_HYPERV;
164783326e43SAndrey Smetanin 		vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
164883326e43SAndrey Smetanin 		vcpu->run->hyperv.u.hcall.input = param;
164983326e43SAndrey Smetanin 		vcpu->run->hyperv.u.hcall.params[0] = ingpa;
165083326e43SAndrey Smetanin 		vcpu->run->hyperv.u.hcall.params[1] = outgpa;
165183326e43SAndrey Smetanin 		vcpu->arch.complete_userspace_io =
165283326e43SAndrey Smetanin 				kvm_hv_hypercall_complete_userspace;
165383326e43SAndrey Smetanin 		return 0;
1654e2f11f42SVitaly Kuznetsov 	case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST:
1655e2f11f42SVitaly Kuznetsov 		if (unlikely(fast || !rep_cnt || rep_idx)) {
1656e2f11f42SVitaly Kuznetsov 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1657e2f11f42SVitaly Kuznetsov 			break;
1658e2f11f42SVitaly Kuznetsov 		}
1659c7012676SVitaly Kuznetsov 		ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, false);
1660e2f11f42SVitaly Kuznetsov 		break;
1661e2f11f42SVitaly Kuznetsov 	case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE:
1662e2f11f42SVitaly Kuznetsov 		if (unlikely(fast || rep)) {
1663e2f11f42SVitaly Kuznetsov 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1664e2f11f42SVitaly Kuznetsov 			break;
1665e2f11f42SVitaly Kuznetsov 		}
1666c7012676SVitaly Kuznetsov 		ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, false);
1667c7012676SVitaly Kuznetsov 		break;
1668c7012676SVitaly Kuznetsov 	case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX:
1669c7012676SVitaly Kuznetsov 		if (unlikely(fast || !rep_cnt || rep_idx)) {
1670c7012676SVitaly Kuznetsov 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1671c7012676SVitaly Kuznetsov 			break;
1672c7012676SVitaly Kuznetsov 		}
1673c7012676SVitaly Kuznetsov 		ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, true);
1674c7012676SVitaly Kuznetsov 		break;
1675c7012676SVitaly Kuznetsov 	case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX:
1676c7012676SVitaly Kuznetsov 		if (unlikely(fast || rep)) {
1677c7012676SVitaly Kuznetsov 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1678c7012676SVitaly Kuznetsov 			break;
1679c7012676SVitaly Kuznetsov 		}
1680c7012676SVitaly Kuznetsov 		ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, true);
1681e2f11f42SVitaly Kuznetsov 		break;
1682214ff83dSVitaly Kuznetsov 	case HVCALL_SEND_IPI:
1683214ff83dSVitaly Kuznetsov 		if (unlikely(rep)) {
1684214ff83dSVitaly Kuznetsov 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1685214ff83dSVitaly Kuznetsov 			break;
1686214ff83dSVitaly Kuznetsov 		}
1687214ff83dSVitaly Kuznetsov 		ret = kvm_hv_send_ipi(vcpu, ingpa, outgpa, false, fast);
1688214ff83dSVitaly Kuznetsov 		break;
1689214ff83dSVitaly Kuznetsov 	case HVCALL_SEND_IPI_EX:
1690214ff83dSVitaly Kuznetsov 		if (unlikely(fast || rep)) {
1691214ff83dSVitaly Kuznetsov 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1692214ff83dSVitaly Kuznetsov 			break;
1693214ff83dSVitaly Kuznetsov 		}
1694214ff83dSVitaly Kuznetsov 		ret = kvm_hv_send_ipi(vcpu, ingpa, outgpa, true, false);
1695214ff83dSVitaly Kuznetsov 		break;
1696e83d5887SAndrey Smetanin 	default:
1697d32ef547SDan Carpenter 		ret = HV_STATUS_INVALID_HYPERCALL_CODE;
1698e83d5887SAndrey Smetanin 		break;
1699e83d5887SAndrey Smetanin 	}
1700e83d5887SAndrey Smetanin 
1701696ca779SRadim Krčmář 	return kvm_hv_hypercall_complete(vcpu, ret);
1702e83d5887SAndrey Smetanin }
1703cbc0236aSRoman Kagan 
1704cbc0236aSRoman Kagan void kvm_hv_init_vm(struct kvm *kvm)
1705cbc0236aSRoman Kagan {
1706cbc0236aSRoman Kagan 	mutex_init(&kvm->arch.hyperv.hv_lock);
1707faeb7833SRoman Kagan 	idr_init(&kvm->arch.hyperv.conn_to_evt);
1708cbc0236aSRoman Kagan }
1709cbc0236aSRoman Kagan 
1710cbc0236aSRoman Kagan void kvm_hv_destroy_vm(struct kvm *kvm)
1711cbc0236aSRoman Kagan {
1712faeb7833SRoman Kagan 	struct eventfd_ctx *eventfd;
1713faeb7833SRoman Kagan 	int i;
1714faeb7833SRoman Kagan 
1715faeb7833SRoman Kagan 	idr_for_each_entry(&kvm->arch.hyperv.conn_to_evt, eventfd, i)
1716faeb7833SRoman Kagan 		eventfd_ctx_put(eventfd);
1717faeb7833SRoman Kagan 	idr_destroy(&kvm->arch.hyperv.conn_to_evt);
1718faeb7833SRoman Kagan }
1719faeb7833SRoman Kagan 
1720faeb7833SRoman Kagan static int kvm_hv_eventfd_assign(struct kvm *kvm, u32 conn_id, int fd)
1721faeb7833SRoman Kagan {
1722faeb7833SRoman Kagan 	struct kvm_hv *hv = &kvm->arch.hyperv;
1723faeb7833SRoman Kagan 	struct eventfd_ctx *eventfd;
1724faeb7833SRoman Kagan 	int ret;
1725faeb7833SRoman Kagan 
1726faeb7833SRoman Kagan 	eventfd = eventfd_ctx_fdget(fd);
1727faeb7833SRoman Kagan 	if (IS_ERR(eventfd))
1728faeb7833SRoman Kagan 		return PTR_ERR(eventfd);
1729faeb7833SRoman Kagan 
1730faeb7833SRoman Kagan 	mutex_lock(&hv->hv_lock);
1731faeb7833SRoman Kagan 	ret = idr_alloc(&hv->conn_to_evt, eventfd, conn_id, conn_id + 1,
1732*254272ceSBen Gardon 			GFP_KERNEL_ACCOUNT);
1733faeb7833SRoman Kagan 	mutex_unlock(&hv->hv_lock);
1734faeb7833SRoman Kagan 
1735faeb7833SRoman Kagan 	if (ret >= 0)
1736faeb7833SRoman Kagan 		return 0;
1737faeb7833SRoman Kagan 
1738faeb7833SRoman Kagan 	if (ret == -ENOSPC)
1739faeb7833SRoman Kagan 		ret = -EEXIST;
1740faeb7833SRoman Kagan 	eventfd_ctx_put(eventfd);
1741faeb7833SRoman Kagan 	return ret;
1742faeb7833SRoman Kagan }
1743faeb7833SRoman Kagan 
1744faeb7833SRoman Kagan static int kvm_hv_eventfd_deassign(struct kvm *kvm, u32 conn_id)
1745faeb7833SRoman Kagan {
1746faeb7833SRoman Kagan 	struct kvm_hv *hv = &kvm->arch.hyperv;
1747faeb7833SRoman Kagan 	struct eventfd_ctx *eventfd;
1748faeb7833SRoman Kagan 
1749faeb7833SRoman Kagan 	mutex_lock(&hv->hv_lock);
1750faeb7833SRoman Kagan 	eventfd = idr_remove(&hv->conn_to_evt, conn_id);
1751faeb7833SRoman Kagan 	mutex_unlock(&hv->hv_lock);
1752faeb7833SRoman Kagan 
1753faeb7833SRoman Kagan 	if (!eventfd)
1754faeb7833SRoman Kagan 		return -ENOENT;
1755faeb7833SRoman Kagan 
1756faeb7833SRoman Kagan 	synchronize_srcu(&kvm->srcu);
1757faeb7833SRoman Kagan 	eventfd_ctx_put(eventfd);
1758faeb7833SRoman Kagan 	return 0;
1759faeb7833SRoman Kagan }
1760faeb7833SRoman Kagan 
1761faeb7833SRoman Kagan int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args)
1762faeb7833SRoman Kagan {
1763faeb7833SRoman Kagan 	if ((args->flags & ~KVM_HYPERV_EVENTFD_DEASSIGN) ||
1764faeb7833SRoman Kagan 	    (args->conn_id & ~KVM_HYPERV_CONN_ID_MASK))
1765faeb7833SRoman Kagan 		return -EINVAL;
1766faeb7833SRoman Kagan 
1767faeb7833SRoman Kagan 	if (args->flags == KVM_HYPERV_EVENTFD_DEASSIGN)
1768faeb7833SRoman Kagan 		return kvm_hv_eventfd_deassign(kvm, args->conn_id);
1769faeb7833SRoman Kagan 	return kvm_hv_eventfd_assign(kvm, args->conn_id, args->fd);
1770cbc0236aSRoman Kagan }
17712bc39970SVitaly Kuznetsov 
17722bc39970SVitaly Kuznetsov int kvm_vcpu_ioctl_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,
17732bc39970SVitaly Kuznetsov 				struct kvm_cpuid_entry2 __user *entries)
17742bc39970SVitaly Kuznetsov {
17752bc39970SVitaly Kuznetsov 	uint16_t evmcs_ver = kvm_x86_ops->nested_get_evmcs_version(vcpu);
17762bc39970SVitaly Kuznetsov 	struct kvm_cpuid_entry2 cpuid_entries[] = {
17772bc39970SVitaly Kuznetsov 		{ .function = HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS },
17782bc39970SVitaly Kuznetsov 		{ .function = HYPERV_CPUID_INTERFACE },
17792bc39970SVitaly Kuznetsov 		{ .function = HYPERV_CPUID_VERSION },
17802bc39970SVitaly Kuznetsov 		{ .function = HYPERV_CPUID_FEATURES },
17812bc39970SVitaly Kuznetsov 		{ .function = HYPERV_CPUID_ENLIGHTMENT_INFO },
17822bc39970SVitaly Kuznetsov 		{ .function = HYPERV_CPUID_IMPLEMENT_LIMITS },
17832bc39970SVitaly Kuznetsov 		{ .function = HYPERV_CPUID_NESTED_FEATURES },
17842bc39970SVitaly Kuznetsov 	};
17852bc39970SVitaly Kuznetsov 	int i, nent = ARRAY_SIZE(cpuid_entries);
17862bc39970SVitaly Kuznetsov 
17872bc39970SVitaly Kuznetsov 	/* Skip NESTED_FEATURES if eVMCS is not supported */
17882bc39970SVitaly Kuznetsov 	if (!evmcs_ver)
17892bc39970SVitaly Kuznetsov 		--nent;
17902bc39970SVitaly Kuznetsov 
17912bc39970SVitaly Kuznetsov 	if (cpuid->nent < nent)
17922bc39970SVitaly Kuznetsov 		return -E2BIG;
17932bc39970SVitaly Kuznetsov 
17942bc39970SVitaly Kuznetsov 	if (cpuid->nent > nent)
17952bc39970SVitaly Kuznetsov 		cpuid->nent = nent;
17962bc39970SVitaly Kuznetsov 
17972bc39970SVitaly Kuznetsov 	for (i = 0; i < nent; i++) {
17982bc39970SVitaly Kuznetsov 		struct kvm_cpuid_entry2 *ent = &cpuid_entries[i];
17992bc39970SVitaly Kuznetsov 		u32 signature[3];
18002bc39970SVitaly Kuznetsov 
18012bc39970SVitaly Kuznetsov 		switch (ent->function) {
18022bc39970SVitaly Kuznetsov 		case HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS:
18032bc39970SVitaly Kuznetsov 			memcpy(signature, "Linux KVM Hv", 12);
18042bc39970SVitaly Kuznetsov 
18052bc39970SVitaly Kuznetsov 			ent->eax = HYPERV_CPUID_NESTED_FEATURES;
18062bc39970SVitaly Kuznetsov 			ent->ebx = signature[0];
18072bc39970SVitaly Kuznetsov 			ent->ecx = signature[1];
18082bc39970SVitaly Kuznetsov 			ent->edx = signature[2];
18092bc39970SVitaly Kuznetsov 			break;
18102bc39970SVitaly Kuznetsov 
18112bc39970SVitaly Kuznetsov 		case HYPERV_CPUID_INTERFACE:
18122bc39970SVitaly Kuznetsov 			memcpy(signature, "Hv#1\0\0\0\0\0\0\0\0", 12);
18132bc39970SVitaly Kuznetsov 			ent->eax = signature[0];
18142bc39970SVitaly Kuznetsov 			break;
18152bc39970SVitaly Kuznetsov 
18162bc39970SVitaly Kuznetsov 		case HYPERV_CPUID_VERSION:
18172bc39970SVitaly Kuznetsov 			/*
18182bc39970SVitaly Kuznetsov 			 * We implement some Hyper-V 2016 functions so let's use
18192bc39970SVitaly Kuznetsov 			 * this version.
18202bc39970SVitaly Kuznetsov 			 */
18212bc39970SVitaly Kuznetsov 			ent->eax = 0x00003839;
18222bc39970SVitaly Kuznetsov 			ent->ebx = 0x000A0000;
18232bc39970SVitaly Kuznetsov 			break;
18242bc39970SVitaly Kuznetsov 
18252bc39970SVitaly Kuznetsov 		case HYPERV_CPUID_FEATURES:
18262bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_MSR_VP_RUNTIME_AVAILABLE;
18272bc39970SVitaly Kuznetsov 			ent->eax |= HV_MSR_TIME_REF_COUNT_AVAILABLE;
18282bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_MSR_SYNIC_AVAILABLE;
18292bc39970SVitaly Kuznetsov 			ent->eax |= HV_MSR_SYNTIMER_AVAILABLE;
18302bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_MSR_APIC_ACCESS_AVAILABLE;
18312bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE;
18322bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_MSR_VP_INDEX_AVAILABLE;
18332bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_MSR_RESET_AVAILABLE;
18342bc39970SVitaly Kuznetsov 			ent->eax |= HV_MSR_REFERENCE_TSC_AVAILABLE;
18352bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_ACCESS_FREQUENCY_MSRS;
18362bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_ACCESS_REENLIGHTENMENT;
18372bc39970SVitaly Kuznetsov 
18382bc39970SVitaly Kuznetsov 			ent->ebx |= HV_X64_POST_MESSAGES;
18392bc39970SVitaly Kuznetsov 			ent->ebx |= HV_X64_SIGNAL_EVENTS;
18402bc39970SVitaly Kuznetsov 
18412bc39970SVitaly Kuznetsov 			ent->edx |= HV_FEATURE_FREQUENCY_MSRS_AVAILABLE;
18422bc39970SVitaly Kuznetsov 			ent->edx |= HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE;
18432bc39970SVitaly Kuznetsov 			ent->edx |= HV_STIMER_DIRECT_MODE_AVAILABLE;
18442bc39970SVitaly Kuznetsov 
18452bc39970SVitaly Kuznetsov 			break;
18462bc39970SVitaly Kuznetsov 
18472bc39970SVitaly Kuznetsov 		case HYPERV_CPUID_ENLIGHTMENT_INFO:
18482bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED;
18492bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_APIC_ACCESS_RECOMMENDED;
18502bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED;
18512bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_CLUSTER_IPI_RECOMMENDED;
18522bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED;
1853f1adceafSVitaly Kuznetsov 			if (evmcs_ver)
18542bc39970SVitaly Kuznetsov 				ent->eax |= HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
18552bc39970SVitaly Kuznetsov 
18562bc39970SVitaly Kuznetsov 			/*
18572bc39970SVitaly Kuznetsov 			 * Default number of spinlock retry attempts, matches
18582bc39970SVitaly Kuznetsov 			 * HyperV 2016.
18592bc39970SVitaly Kuznetsov 			 */
18602bc39970SVitaly Kuznetsov 			ent->ebx = 0x00000FFF;
18612bc39970SVitaly Kuznetsov 
18622bc39970SVitaly Kuznetsov 			break;
18632bc39970SVitaly Kuznetsov 
18642bc39970SVitaly Kuznetsov 		case HYPERV_CPUID_IMPLEMENT_LIMITS:
18652bc39970SVitaly Kuznetsov 			/* Maximum number of virtual processors */
18662bc39970SVitaly Kuznetsov 			ent->eax = KVM_MAX_VCPUS;
18672bc39970SVitaly Kuznetsov 			/*
18682bc39970SVitaly Kuznetsov 			 * Maximum number of logical processors, matches
18692bc39970SVitaly Kuznetsov 			 * HyperV 2016.
18702bc39970SVitaly Kuznetsov 			 */
18712bc39970SVitaly Kuznetsov 			ent->ebx = 64;
18722bc39970SVitaly Kuznetsov 
18732bc39970SVitaly Kuznetsov 			break;
18742bc39970SVitaly Kuznetsov 
18752bc39970SVitaly Kuznetsov 		case HYPERV_CPUID_NESTED_FEATURES:
18762bc39970SVitaly Kuznetsov 			ent->eax = evmcs_ver;
18772bc39970SVitaly Kuznetsov 
18782bc39970SVitaly Kuznetsov 			break;
18792bc39970SVitaly Kuznetsov 
18802bc39970SVitaly Kuznetsov 		default:
18812bc39970SVitaly Kuznetsov 			break;
18822bc39970SVitaly Kuznetsov 		}
18832bc39970SVitaly Kuznetsov 	}
18842bc39970SVitaly Kuznetsov 
18852bc39970SVitaly Kuznetsov 	if (copy_to_user(entries, cpuid_entries,
18862bc39970SVitaly Kuznetsov 			 nent * sizeof(struct kvm_cpuid_entry2)))
18872bc39970SVitaly Kuznetsov 		return -EFAULT;
18882bc39970SVitaly Kuznetsov 
18892bc39970SVitaly Kuznetsov 	return 0;
18902bc39970SVitaly Kuznetsov }
1891