xref: /openbmc/linux/arch/x86/kvm/hyperv.c (revision f4fdc0a2edf48f16f7b10cceaf4781fc56ab7fd9)
120c8ccb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e83d5887SAndrey Smetanin /*
3e83d5887SAndrey Smetanin  * KVM Microsoft Hyper-V emulation
4e83d5887SAndrey Smetanin  *
5e83d5887SAndrey Smetanin  * derived from arch/x86/kvm/x86.c
6e83d5887SAndrey Smetanin  *
7e83d5887SAndrey Smetanin  * Copyright (C) 2006 Qumranet, Inc.
8e83d5887SAndrey Smetanin  * Copyright (C) 2008 Qumranet, Inc.
9e83d5887SAndrey Smetanin  * Copyright IBM Corporation, 2008
10e83d5887SAndrey Smetanin  * Copyright 2010 Red Hat, Inc. and/or its affiliates.
11e83d5887SAndrey Smetanin  * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com>
12e83d5887SAndrey Smetanin  *
13e83d5887SAndrey Smetanin  * Authors:
14e83d5887SAndrey Smetanin  *   Avi Kivity   <avi@qumranet.com>
15e83d5887SAndrey Smetanin  *   Yaniv Kamay  <yaniv@qumranet.com>
16e83d5887SAndrey Smetanin  *   Amit Shah    <amit.shah@qumranet.com>
17e83d5887SAndrey Smetanin  *   Ben-Ami Yassour <benami@il.ibm.com>
18e83d5887SAndrey Smetanin  *   Andrey Smetanin <asmetanin@virtuozzo.com>
19e83d5887SAndrey Smetanin  */
20e83d5887SAndrey Smetanin 
21e83d5887SAndrey Smetanin #include "x86.h"
22e83d5887SAndrey Smetanin #include "lapic.h"
235c919412SAndrey Smetanin #include "ioapic.h"
24e83d5887SAndrey Smetanin #include "hyperv.h"
25e83d5887SAndrey Smetanin 
26b2d8b167SVitaly Kuznetsov #include <linux/cpu.h>
27e83d5887SAndrey Smetanin #include <linux/kvm_host.h>
28765eaa0fSAndrey Smetanin #include <linux/highmem.h>
2932ef5517SIngo Molnar #include <linux/sched/cputime.h>
30faeb7833SRoman Kagan #include <linux/eventfd.h>
3132ef5517SIngo Molnar 
325c919412SAndrey Smetanin #include <asm/apicdef.h>
33e83d5887SAndrey Smetanin #include <trace/events/kvm.h>
34e83d5887SAndrey Smetanin 
35e83d5887SAndrey Smetanin #include "trace.h"
3659508b30SPeter Xu #include "irq.h"
37e83d5887SAndrey Smetanin 
38f21dd494SVitaly Kuznetsov #define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, 64)
39f21dd494SVitaly Kuznetsov 
408644f771SVitaly Kuznetsov static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
418644f771SVitaly Kuznetsov 				bool vcpu_kick);
428644f771SVitaly Kuznetsov 
435c919412SAndrey Smetanin static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
445c919412SAndrey Smetanin {
455c919412SAndrey Smetanin 	return atomic64_read(&synic->sint[sint]);
465c919412SAndrey Smetanin }
475c919412SAndrey Smetanin 
485c919412SAndrey Smetanin static inline int synic_get_sint_vector(u64 sint_value)
495c919412SAndrey Smetanin {
505c919412SAndrey Smetanin 	if (sint_value & HV_SYNIC_SINT_MASKED)
515c919412SAndrey Smetanin 		return -1;
525c919412SAndrey Smetanin 	return sint_value & HV_SYNIC_SINT_VECTOR_MASK;
535c919412SAndrey Smetanin }
545c919412SAndrey Smetanin 
555c919412SAndrey Smetanin static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic,
565c919412SAndrey Smetanin 				      int vector)
575c919412SAndrey Smetanin {
585c919412SAndrey Smetanin 	int i;
595c919412SAndrey Smetanin 
605c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
615c919412SAndrey Smetanin 		if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
625c919412SAndrey Smetanin 			return true;
635c919412SAndrey Smetanin 	}
645c919412SAndrey Smetanin 	return false;
655c919412SAndrey Smetanin }
665c919412SAndrey Smetanin 
675c919412SAndrey Smetanin static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic,
685c919412SAndrey Smetanin 				     int vector)
695c919412SAndrey Smetanin {
705c919412SAndrey Smetanin 	int i;
715c919412SAndrey Smetanin 	u64 sint_value;
725c919412SAndrey Smetanin 
735c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
745c919412SAndrey Smetanin 		sint_value = synic_read_sint(synic, i);
755c919412SAndrey Smetanin 		if (synic_get_sint_vector(sint_value) == vector &&
765c919412SAndrey Smetanin 		    sint_value & HV_SYNIC_SINT_AUTO_EOI)
775c919412SAndrey Smetanin 			return true;
785c919412SAndrey Smetanin 	}
795c919412SAndrey Smetanin 	return false;
805c919412SAndrey Smetanin }
815c919412SAndrey Smetanin 
8298f65ad4SVitaly Kuznetsov static void synic_update_vector(struct kvm_vcpu_hv_synic *synic,
8398f65ad4SVitaly Kuznetsov 				int vector)
845c919412SAndrey Smetanin {
8587a8d795SVitaly Kuznetsov 	if (vector < HV_SYNIC_FIRST_VALID_VECTOR)
8687a8d795SVitaly Kuznetsov 		return;
8787a8d795SVitaly Kuznetsov 
885c919412SAndrey Smetanin 	if (synic_has_vector_connected(synic, vector))
895c919412SAndrey Smetanin 		__set_bit(vector, synic->vec_bitmap);
905c919412SAndrey Smetanin 	else
915c919412SAndrey Smetanin 		__clear_bit(vector, synic->vec_bitmap);
925c919412SAndrey Smetanin 
935c919412SAndrey Smetanin 	if (synic_has_vector_auto_eoi(synic, vector))
945c919412SAndrey Smetanin 		__set_bit(vector, synic->auto_eoi_bitmap);
955c919412SAndrey Smetanin 	else
965c919412SAndrey Smetanin 		__clear_bit(vector, synic->auto_eoi_bitmap);
9798f65ad4SVitaly Kuznetsov }
9898f65ad4SVitaly Kuznetsov 
9998f65ad4SVitaly Kuznetsov static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint,
10098f65ad4SVitaly Kuznetsov 			  u64 data, bool host)
10198f65ad4SVitaly Kuznetsov {
10298f65ad4SVitaly Kuznetsov 	int vector, old_vector;
103915e6f78SVitaly Kuznetsov 	bool masked;
10498f65ad4SVitaly Kuznetsov 
10598f65ad4SVitaly Kuznetsov 	vector = data & HV_SYNIC_SINT_VECTOR_MASK;
106915e6f78SVitaly Kuznetsov 	masked = data & HV_SYNIC_SINT_MASKED;
107915e6f78SVitaly Kuznetsov 
108915e6f78SVitaly Kuznetsov 	/*
109915e6f78SVitaly Kuznetsov 	 * Valid vectors are 16-255, however, nested Hyper-V attempts to write
110915e6f78SVitaly Kuznetsov 	 * default '0x10000' value on boot and this should not #GP. We need to
111915e6f78SVitaly Kuznetsov 	 * allow zero-initing the register from host as well.
112915e6f78SVitaly Kuznetsov 	 */
113915e6f78SVitaly Kuznetsov 	if (vector < HV_SYNIC_FIRST_VALID_VECTOR && !host && !masked)
11498f65ad4SVitaly Kuznetsov 		return 1;
11598f65ad4SVitaly Kuznetsov 	/*
11698f65ad4SVitaly Kuznetsov 	 * Guest may configure multiple SINTs to use the same vector, so
11798f65ad4SVitaly Kuznetsov 	 * we maintain a bitmap of vectors handled by synic, and a
11898f65ad4SVitaly Kuznetsov 	 * bitmap of vectors with auto-eoi behavior.  The bitmaps are
11998f65ad4SVitaly Kuznetsov 	 * updated here, and atomically queried on fast paths.
12098f65ad4SVitaly Kuznetsov 	 */
12198f65ad4SVitaly Kuznetsov 	old_vector = synic_read_sint(synic, sint) & HV_SYNIC_SINT_VECTOR_MASK;
12298f65ad4SVitaly Kuznetsov 
12398f65ad4SVitaly Kuznetsov 	atomic64_set(&synic->sint[sint], data);
12498f65ad4SVitaly Kuznetsov 
12598f65ad4SVitaly Kuznetsov 	synic_update_vector(synic, old_vector);
12698f65ad4SVitaly Kuznetsov 
12798f65ad4SVitaly Kuznetsov 	synic_update_vector(synic, vector);
1285c919412SAndrey Smetanin 
1295c919412SAndrey Smetanin 	/* Load SynIC vectors into EOI exit bitmap */
1305c919412SAndrey Smetanin 	kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic));
1315c919412SAndrey Smetanin 	return 0;
1325c919412SAndrey Smetanin }
1335c919412SAndrey Smetanin 
134d3457c87SRoman Kagan static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx)
135d3457c87SRoman Kagan {
136d3457c87SRoman Kagan 	struct kvm_vcpu *vcpu = NULL;
137d3457c87SRoman Kagan 	int i;
138d3457c87SRoman Kagan 
1399170200eSVitaly Kuznetsov 	if (vpidx >= KVM_MAX_VCPUS)
1409170200eSVitaly Kuznetsov 		return NULL;
1419170200eSVitaly Kuznetsov 
142d3457c87SRoman Kagan 	vcpu = kvm_get_vcpu(kvm, vpidx);
143d3457c87SRoman Kagan 	if (vcpu && vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx)
144d3457c87SRoman Kagan 		return vcpu;
145d3457c87SRoman Kagan 	kvm_for_each_vcpu(i, vcpu, kvm)
146d3457c87SRoman Kagan 		if (vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx)
147d3457c87SRoman Kagan 			return vcpu;
148d3457c87SRoman Kagan 	return NULL;
149d3457c87SRoman Kagan }
150d3457c87SRoman Kagan 
151d3457c87SRoman Kagan static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vpidx)
1525c919412SAndrey Smetanin {
1535c919412SAndrey Smetanin 	struct kvm_vcpu *vcpu;
1545c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic;
1555c919412SAndrey Smetanin 
156d3457c87SRoman Kagan 	vcpu = get_vcpu_by_vpidx(kvm, vpidx);
1575c919412SAndrey Smetanin 	if (!vcpu)
1585c919412SAndrey Smetanin 		return NULL;
1595c919412SAndrey Smetanin 	synic = vcpu_to_synic(vcpu);
1605c919412SAndrey Smetanin 	return (synic->active) ? synic : NULL;
1615c919412SAndrey Smetanin }
1625c919412SAndrey Smetanin 
1635c919412SAndrey Smetanin static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
1645c919412SAndrey Smetanin {
1655c919412SAndrey Smetanin 	struct kvm *kvm = vcpu->kvm;
166765eaa0fSAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
1671f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
1681f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv_stimer *stimer;
16908a800acSVitaly Kuznetsov 	int gsi, idx;
1705c919412SAndrey Smetanin 
17118659a9cSAndrey Smetanin 	trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint);
1725c919412SAndrey Smetanin 
1731f4b34f8SAndrey Smetanin 	/* Try to deliver pending Hyper-V SynIC timers messages */
1741f4b34f8SAndrey Smetanin 	for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) {
1751f4b34f8SAndrey Smetanin 		stimer = &hv_vcpu->stimer[idx];
1766a058a1eSVitaly Kuznetsov 		if (stimer->msg_pending && stimer->config.enable &&
1778644f771SVitaly Kuznetsov 		    !stimer->config.direct_mode &&
17808a800acSVitaly Kuznetsov 		    stimer->config.sintx == sint)
17908a800acSVitaly Kuznetsov 			stimer_mark_pending(stimer, false);
1801f4b34f8SAndrey Smetanin 	}
1811f4b34f8SAndrey Smetanin 
1825c919412SAndrey Smetanin 	idx = srcu_read_lock(&kvm->irq_srcu);
1831f4b34f8SAndrey Smetanin 	gsi = atomic_read(&synic->sint_to_gsi[sint]);
1845c919412SAndrey Smetanin 	if (gsi != -1)
1855c919412SAndrey Smetanin 		kvm_notify_acked_gsi(kvm, gsi);
1865c919412SAndrey Smetanin 	srcu_read_unlock(&kvm->irq_srcu, idx);
1875c919412SAndrey Smetanin }
1885c919412SAndrey Smetanin 
189db397571SAndrey Smetanin static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
190db397571SAndrey Smetanin {
191db397571SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
192db397571SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
193db397571SAndrey Smetanin 
194db397571SAndrey Smetanin 	hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
195db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.msr = msr;
196db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.control = synic->control;
197db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
198db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
199db397571SAndrey Smetanin 
200db397571SAndrey Smetanin 	kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
201db397571SAndrey Smetanin }
202db397571SAndrey Smetanin 
2035c919412SAndrey Smetanin static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
2045c919412SAndrey Smetanin 			 u32 msr, u64 data, bool host)
2055c919412SAndrey Smetanin {
2065c919412SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
2075c919412SAndrey Smetanin 	int ret;
2085c919412SAndrey Smetanin 
20944883f01SPaolo Bonzini 	if (!synic->active && !host)
2105c919412SAndrey Smetanin 		return 1;
2115c919412SAndrey Smetanin 
21218659a9cSAndrey Smetanin 	trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host);
21318659a9cSAndrey Smetanin 
2145c919412SAndrey Smetanin 	ret = 0;
2155c919412SAndrey Smetanin 	switch (msr) {
2165c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
2175c919412SAndrey Smetanin 		synic->control = data;
218db397571SAndrey Smetanin 		if (!host)
219db397571SAndrey Smetanin 			synic_exit(synic, msr);
2205c919412SAndrey Smetanin 		break;
2215c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
2225c919412SAndrey Smetanin 		if (!host) {
2235c919412SAndrey Smetanin 			ret = 1;
2245c919412SAndrey Smetanin 			break;
2255c919412SAndrey Smetanin 		}
2265c919412SAndrey Smetanin 		synic->version = data;
2275c919412SAndrey Smetanin 		break;
2285c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
229efc479e6SRoman Kagan 		if ((data & HV_SYNIC_SIEFP_ENABLE) && !host &&
230efc479e6SRoman Kagan 		    !synic->dont_zero_synic_pages)
2315c919412SAndrey Smetanin 			if (kvm_clear_guest(vcpu->kvm,
2325c919412SAndrey Smetanin 					    data & PAGE_MASK, PAGE_SIZE)) {
2335c919412SAndrey Smetanin 				ret = 1;
2345c919412SAndrey Smetanin 				break;
2355c919412SAndrey Smetanin 			}
2365c919412SAndrey Smetanin 		synic->evt_page = data;
237db397571SAndrey Smetanin 		if (!host)
238db397571SAndrey Smetanin 			synic_exit(synic, msr);
2395c919412SAndrey Smetanin 		break;
2405c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
241efc479e6SRoman Kagan 		if ((data & HV_SYNIC_SIMP_ENABLE) && !host &&
242efc479e6SRoman Kagan 		    !synic->dont_zero_synic_pages)
2435c919412SAndrey Smetanin 			if (kvm_clear_guest(vcpu->kvm,
2445c919412SAndrey Smetanin 					    data & PAGE_MASK, PAGE_SIZE)) {
2455c919412SAndrey Smetanin 				ret = 1;
2465c919412SAndrey Smetanin 				break;
2475c919412SAndrey Smetanin 			}
2485c919412SAndrey Smetanin 		synic->msg_page = data;
249db397571SAndrey Smetanin 		if (!host)
250db397571SAndrey Smetanin 			synic_exit(synic, msr);
2515c919412SAndrey Smetanin 		break;
2525c919412SAndrey Smetanin 	case HV_X64_MSR_EOM: {
2535c919412SAndrey Smetanin 		int i;
2545c919412SAndrey Smetanin 
2555c919412SAndrey Smetanin 		for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
2565c919412SAndrey Smetanin 			kvm_hv_notify_acked_sint(vcpu, i);
2575c919412SAndrey Smetanin 		break;
2585c919412SAndrey Smetanin 	}
2595c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
2607be58a64SAndrey Smetanin 		ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host);
2615c919412SAndrey Smetanin 		break;
2625c919412SAndrey Smetanin 	default:
2635c919412SAndrey Smetanin 		ret = 1;
2645c919412SAndrey Smetanin 		break;
2655c919412SAndrey Smetanin 	}
2665c919412SAndrey Smetanin 	return ret;
2675c919412SAndrey Smetanin }
2685c919412SAndrey Smetanin 
26944883f01SPaolo Bonzini static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata,
27044883f01SPaolo Bonzini 			 bool host)
2715c919412SAndrey Smetanin {
2725c919412SAndrey Smetanin 	int ret;
2735c919412SAndrey Smetanin 
27444883f01SPaolo Bonzini 	if (!synic->active && !host)
2755c919412SAndrey Smetanin 		return 1;
2765c919412SAndrey Smetanin 
2775c919412SAndrey Smetanin 	ret = 0;
2785c919412SAndrey Smetanin 	switch (msr) {
2795c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
2805c919412SAndrey Smetanin 		*pdata = synic->control;
2815c919412SAndrey Smetanin 		break;
2825c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
2835c919412SAndrey Smetanin 		*pdata = synic->version;
2845c919412SAndrey Smetanin 		break;
2855c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
2865c919412SAndrey Smetanin 		*pdata = synic->evt_page;
2875c919412SAndrey Smetanin 		break;
2885c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
2895c919412SAndrey Smetanin 		*pdata = synic->msg_page;
2905c919412SAndrey Smetanin 		break;
2915c919412SAndrey Smetanin 	case HV_X64_MSR_EOM:
2925c919412SAndrey Smetanin 		*pdata = 0;
2935c919412SAndrey Smetanin 		break;
2945c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
2955c919412SAndrey Smetanin 		*pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
2965c919412SAndrey Smetanin 		break;
2975c919412SAndrey Smetanin 	default:
2985c919412SAndrey Smetanin 		ret = 1;
2995c919412SAndrey Smetanin 		break;
3005c919412SAndrey Smetanin 	}
3015c919412SAndrey Smetanin 	return ret;
3025c919412SAndrey Smetanin }
3035c919412SAndrey Smetanin 
304ecd8a8c2SJiang Biao static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
3055c919412SAndrey Smetanin {
3065c919412SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
3075c919412SAndrey Smetanin 	struct kvm_lapic_irq irq;
3085c919412SAndrey Smetanin 	int ret, vector;
3095c919412SAndrey Smetanin 
3105c919412SAndrey Smetanin 	if (sint >= ARRAY_SIZE(synic->sint))
3115c919412SAndrey Smetanin 		return -EINVAL;
3125c919412SAndrey Smetanin 
3135c919412SAndrey Smetanin 	vector = synic_get_sint_vector(synic_read_sint(synic, sint));
3145c919412SAndrey Smetanin 	if (vector < 0)
3155c919412SAndrey Smetanin 		return -ENOENT;
3165c919412SAndrey Smetanin 
3175c919412SAndrey Smetanin 	memset(&irq, 0, sizeof(irq));
318f98a3efbSRadim Krčmář 	irq.shorthand = APIC_DEST_SELF;
3195c919412SAndrey Smetanin 	irq.dest_mode = APIC_DEST_PHYSICAL;
3205c919412SAndrey Smetanin 	irq.delivery_mode = APIC_DM_FIXED;
3215c919412SAndrey Smetanin 	irq.vector = vector;
3225c919412SAndrey Smetanin 	irq.level = 1;
3235c919412SAndrey Smetanin 
324f98a3efbSRadim Krčmář 	ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq, NULL);
32518659a9cSAndrey Smetanin 	trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret);
3265c919412SAndrey Smetanin 	return ret;
3275c919412SAndrey Smetanin }
3285c919412SAndrey Smetanin 
329d3457c87SRoman Kagan int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vpidx, u32 sint)
3305c919412SAndrey Smetanin {
3315c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic;
3325c919412SAndrey Smetanin 
333d3457c87SRoman Kagan 	synic = synic_get(kvm, vpidx);
3345c919412SAndrey Smetanin 	if (!synic)
3355c919412SAndrey Smetanin 		return -EINVAL;
3365c919412SAndrey Smetanin 
3375c919412SAndrey Smetanin 	return synic_set_irq(synic, sint);
3385c919412SAndrey Smetanin }
3395c919412SAndrey Smetanin 
3405c919412SAndrey Smetanin void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
3415c919412SAndrey Smetanin {
3425c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
3435c919412SAndrey Smetanin 	int i;
3445c919412SAndrey Smetanin 
34518659a9cSAndrey Smetanin 	trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector);
3465c919412SAndrey Smetanin 
3475c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
3485c919412SAndrey Smetanin 		if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
3495c919412SAndrey Smetanin 			kvm_hv_notify_acked_sint(vcpu, i);
3505c919412SAndrey Smetanin }
3515c919412SAndrey Smetanin 
352d3457c87SRoman Kagan static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vpidx, u32 sint, int gsi)
3535c919412SAndrey Smetanin {
3545c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic;
3555c919412SAndrey Smetanin 
356d3457c87SRoman Kagan 	synic = synic_get(kvm, vpidx);
3575c919412SAndrey Smetanin 	if (!synic)
3585c919412SAndrey Smetanin 		return -EINVAL;
3595c919412SAndrey Smetanin 
3605c919412SAndrey Smetanin 	if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
3615c919412SAndrey Smetanin 		return -EINVAL;
3625c919412SAndrey Smetanin 
3635c919412SAndrey Smetanin 	atomic_set(&synic->sint_to_gsi[sint], gsi);
3645c919412SAndrey Smetanin 	return 0;
3655c919412SAndrey Smetanin }
3665c919412SAndrey Smetanin 
3675c919412SAndrey Smetanin void kvm_hv_irq_routing_update(struct kvm *kvm)
3685c919412SAndrey Smetanin {
3695c919412SAndrey Smetanin 	struct kvm_irq_routing_table *irq_rt;
3705c919412SAndrey Smetanin 	struct kvm_kernel_irq_routing_entry *e;
3715c919412SAndrey Smetanin 	u32 gsi;
3725c919412SAndrey Smetanin 
3735c919412SAndrey Smetanin 	irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
3745c919412SAndrey Smetanin 					lockdep_is_held(&kvm->irq_lock));
3755c919412SAndrey Smetanin 
3765c919412SAndrey Smetanin 	for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
3775c919412SAndrey Smetanin 		hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
3785c919412SAndrey Smetanin 			if (e->type == KVM_IRQ_ROUTING_HV_SINT)
3795c919412SAndrey Smetanin 				kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
3805c919412SAndrey Smetanin 						    e->hv_sint.sint, gsi);
3815c919412SAndrey Smetanin 		}
3825c919412SAndrey Smetanin 	}
3835c919412SAndrey Smetanin }
3845c919412SAndrey Smetanin 
3855c919412SAndrey Smetanin static void synic_init(struct kvm_vcpu_hv_synic *synic)
3865c919412SAndrey Smetanin {
3875c919412SAndrey Smetanin 	int i;
3885c919412SAndrey Smetanin 
3895c919412SAndrey Smetanin 	memset(synic, 0, sizeof(*synic));
3905c919412SAndrey Smetanin 	synic->version = HV_SYNIC_VERSION_1;
3915c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
3925c919412SAndrey Smetanin 		atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
3935c919412SAndrey Smetanin 		atomic_set(&synic->sint_to_gsi[i], -1);
3945c919412SAndrey Smetanin 	}
3955c919412SAndrey Smetanin }
3965c919412SAndrey Smetanin 
39793bf4172SAndrey Smetanin static u64 get_time_ref_counter(struct kvm *kvm)
39893bf4172SAndrey Smetanin {
399095cf55dSPaolo Bonzini 	struct kvm_hv *hv = &kvm->arch.hyperv;
400095cf55dSPaolo Bonzini 	struct kvm_vcpu *vcpu;
401095cf55dSPaolo Bonzini 	u64 tsc;
402095cf55dSPaolo Bonzini 
403095cf55dSPaolo Bonzini 	/*
404095cf55dSPaolo Bonzini 	 * The guest has not set up the TSC page or the clock isn't
405095cf55dSPaolo Bonzini 	 * stable, fall back to get_kvmclock_ns.
406095cf55dSPaolo Bonzini 	 */
407095cf55dSPaolo Bonzini 	if (!hv->tsc_ref.tsc_sequence)
408108b249cSPaolo Bonzini 		return div_u64(get_kvmclock_ns(kvm), 100);
409095cf55dSPaolo Bonzini 
410095cf55dSPaolo Bonzini 	vcpu = kvm_get_vcpu(kvm, 0);
411095cf55dSPaolo Bonzini 	tsc = kvm_read_l1_tsc(vcpu, rdtsc());
412095cf55dSPaolo Bonzini 	return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64)
413095cf55dSPaolo Bonzini 		+ hv->tsc_ref.tsc_offset;
41493bf4172SAndrey Smetanin }
41593bf4172SAndrey Smetanin 
416f3b138c5SAndrey Smetanin static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
4171f4b34f8SAndrey Smetanin 				bool vcpu_kick)
4181f4b34f8SAndrey Smetanin {
4191f4b34f8SAndrey Smetanin 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
4201f4b34f8SAndrey Smetanin 
4211f4b34f8SAndrey Smetanin 	set_bit(stimer->index,
4221f4b34f8SAndrey Smetanin 		vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
4231f4b34f8SAndrey Smetanin 	kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
4241f4b34f8SAndrey Smetanin 	if (vcpu_kick)
4251f4b34f8SAndrey Smetanin 		kvm_vcpu_kick(vcpu);
4261f4b34f8SAndrey Smetanin }
4271f4b34f8SAndrey Smetanin 
4281f4b34f8SAndrey Smetanin static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
4291f4b34f8SAndrey Smetanin {
4301f4b34f8SAndrey Smetanin 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
4311f4b34f8SAndrey Smetanin 
432ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_cleanup(stimer_to_vcpu(stimer)->vcpu_id,
433ac3e5fcaSAndrey Smetanin 				    stimer->index);
434ac3e5fcaSAndrey Smetanin 
435019b9781SAndrey Smetanin 	hrtimer_cancel(&stimer->timer);
4361f4b34f8SAndrey Smetanin 	clear_bit(stimer->index,
4371f4b34f8SAndrey Smetanin 		  vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
4381f4b34f8SAndrey Smetanin 	stimer->msg_pending = false;
439f808495dSAndrey Smetanin 	stimer->exp_time = 0;
4401f4b34f8SAndrey Smetanin }
4411f4b34f8SAndrey Smetanin 
4421f4b34f8SAndrey Smetanin static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
4431f4b34f8SAndrey Smetanin {
4441f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv_stimer *stimer;
4451f4b34f8SAndrey Smetanin 
4461f4b34f8SAndrey Smetanin 	stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
447ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_callback(stimer_to_vcpu(stimer)->vcpu_id,
448ac3e5fcaSAndrey Smetanin 				     stimer->index);
449f3b138c5SAndrey Smetanin 	stimer_mark_pending(stimer, true);
4501f4b34f8SAndrey Smetanin 
4511f4b34f8SAndrey Smetanin 	return HRTIMER_NORESTART;
4521f4b34f8SAndrey Smetanin }
4531f4b34f8SAndrey Smetanin 
454f808495dSAndrey Smetanin /*
455f808495dSAndrey Smetanin  * stimer_start() assumptions:
456f808495dSAndrey Smetanin  * a) stimer->count is not equal to 0
457f808495dSAndrey Smetanin  * b) stimer->config has HV_STIMER_ENABLE flag
458f808495dSAndrey Smetanin  */
4591f4b34f8SAndrey Smetanin static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
4601f4b34f8SAndrey Smetanin {
4611f4b34f8SAndrey Smetanin 	u64 time_now;
4621f4b34f8SAndrey Smetanin 	ktime_t ktime_now;
4631f4b34f8SAndrey Smetanin 
4641f4b34f8SAndrey Smetanin 	time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm);
4651f4b34f8SAndrey Smetanin 	ktime_now = ktime_get();
4661f4b34f8SAndrey Smetanin 
4676a058a1eSVitaly Kuznetsov 	if (stimer->config.periodic) {
468f808495dSAndrey Smetanin 		if (stimer->exp_time) {
469f808495dSAndrey Smetanin 			if (time_now >= stimer->exp_time) {
470f808495dSAndrey Smetanin 				u64 remainder;
4711f4b34f8SAndrey Smetanin 
472f808495dSAndrey Smetanin 				div64_u64_rem(time_now - stimer->exp_time,
473f808495dSAndrey Smetanin 					      stimer->count, &remainder);
474f808495dSAndrey Smetanin 				stimer->exp_time =
475f808495dSAndrey Smetanin 					time_now + (stimer->count - remainder);
476f808495dSAndrey Smetanin 			}
477f808495dSAndrey Smetanin 		} else
4781f4b34f8SAndrey Smetanin 			stimer->exp_time = time_now + stimer->count;
479f808495dSAndrey Smetanin 
480ac3e5fcaSAndrey Smetanin 		trace_kvm_hv_stimer_start_periodic(
481ac3e5fcaSAndrey Smetanin 					stimer_to_vcpu(stimer)->vcpu_id,
482ac3e5fcaSAndrey Smetanin 					stimer->index,
483ac3e5fcaSAndrey Smetanin 					time_now, stimer->exp_time);
484ac3e5fcaSAndrey Smetanin 
4851f4b34f8SAndrey Smetanin 		hrtimer_start(&stimer->timer,
486f808495dSAndrey Smetanin 			      ktime_add_ns(ktime_now,
487f808495dSAndrey Smetanin 					   100 * (stimer->exp_time - time_now)),
4881f4b34f8SAndrey Smetanin 			      HRTIMER_MODE_ABS);
4891f4b34f8SAndrey Smetanin 		return 0;
4901f4b34f8SAndrey Smetanin 	}
4911f4b34f8SAndrey Smetanin 	stimer->exp_time = stimer->count;
4921f4b34f8SAndrey Smetanin 	if (time_now >= stimer->count) {
4931f4b34f8SAndrey Smetanin 		/*
4941f4b34f8SAndrey Smetanin 		 * Expire timer according to Hypervisor Top-Level Functional
4951f4b34f8SAndrey Smetanin 		 * specification v4(15.3.1):
4961f4b34f8SAndrey Smetanin 		 * "If a one shot is enabled and the specified count is in
4971f4b34f8SAndrey Smetanin 		 * the past, it will expire immediately."
4981f4b34f8SAndrey Smetanin 		 */
499f3b138c5SAndrey Smetanin 		stimer_mark_pending(stimer, false);
5001f4b34f8SAndrey Smetanin 		return 0;
5011f4b34f8SAndrey Smetanin 	}
5021f4b34f8SAndrey Smetanin 
503ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_start_one_shot(stimer_to_vcpu(stimer)->vcpu_id,
504ac3e5fcaSAndrey Smetanin 					   stimer->index,
505ac3e5fcaSAndrey Smetanin 					   time_now, stimer->count);
506ac3e5fcaSAndrey Smetanin 
5071f4b34f8SAndrey Smetanin 	hrtimer_start(&stimer->timer,
5081f4b34f8SAndrey Smetanin 		      ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
5091f4b34f8SAndrey Smetanin 		      HRTIMER_MODE_ABS);
5101f4b34f8SAndrey Smetanin 	return 0;
5111f4b34f8SAndrey Smetanin }
5121f4b34f8SAndrey Smetanin 
5131f4b34f8SAndrey Smetanin static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
5141f4b34f8SAndrey Smetanin 			     bool host)
5151f4b34f8SAndrey Smetanin {
5168644f771SVitaly Kuznetsov 	union hv_stimer_config new_config = {.as_uint64 = config},
5178644f771SVitaly Kuznetsov 		old_config = {.as_uint64 = stimer->config.as_uint64};
5186a058a1eSVitaly Kuznetsov 
519ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_set_config(stimer_to_vcpu(stimer)->vcpu_id,
520ac3e5fcaSAndrey Smetanin 				       stimer->index, config, host);
521ac3e5fcaSAndrey Smetanin 
522f3b138c5SAndrey Smetanin 	stimer_cleanup(stimer);
5238644f771SVitaly Kuznetsov 	if (old_config.enable &&
5248644f771SVitaly Kuznetsov 	    !new_config.direct_mode && new_config.sintx == 0)
5256a058a1eSVitaly Kuznetsov 		new_config.enable = 0;
5266a058a1eSVitaly Kuznetsov 	stimer->config.as_uint64 = new_config.as_uint64;
5278644f771SVitaly Kuznetsov 
528013cc6ebSVitaly Kuznetsov 	if (stimer->config.enable)
529f3b138c5SAndrey Smetanin 		stimer_mark_pending(stimer, false);
530013cc6ebSVitaly Kuznetsov 
5311f4b34f8SAndrey Smetanin 	return 0;
5321f4b34f8SAndrey Smetanin }
5331f4b34f8SAndrey Smetanin 
5341f4b34f8SAndrey Smetanin static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
5351f4b34f8SAndrey Smetanin 			    bool host)
5361f4b34f8SAndrey Smetanin {
537ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_set_count(stimer_to_vcpu(stimer)->vcpu_id,
538ac3e5fcaSAndrey Smetanin 				      stimer->index, count, host);
539ac3e5fcaSAndrey Smetanin 
5401f4b34f8SAndrey Smetanin 	stimer_cleanup(stimer);
541f3b138c5SAndrey Smetanin 	stimer->count = count;
5421f4b34f8SAndrey Smetanin 	if (stimer->count == 0)
5436a058a1eSVitaly Kuznetsov 		stimer->config.enable = 0;
5446a058a1eSVitaly Kuznetsov 	else if (stimer->config.auto_enable)
5456a058a1eSVitaly Kuznetsov 		stimer->config.enable = 1;
546013cc6ebSVitaly Kuznetsov 
547013cc6ebSVitaly Kuznetsov 	if (stimer->config.enable)
548f3b138c5SAndrey Smetanin 		stimer_mark_pending(stimer, false);
549013cc6ebSVitaly Kuznetsov 
5501f4b34f8SAndrey Smetanin 	return 0;
5511f4b34f8SAndrey Smetanin }
5521f4b34f8SAndrey Smetanin 
5531f4b34f8SAndrey Smetanin static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
5541f4b34f8SAndrey Smetanin {
5556a058a1eSVitaly Kuznetsov 	*pconfig = stimer->config.as_uint64;
5561f4b34f8SAndrey Smetanin 	return 0;
5571f4b34f8SAndrey Smetanin }
5581f4b34f8SAndrey Smetanin 
5591f4b34f8SAndrey Smetanin static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
5601f4b34f8SAndrey Smetanin {
5611f4b34f8SAndrey Smetanin 	*pcount = stimer->count;
5621f4b34f8SAndrey Smetanin 	return 0;
5631f4b34f8SAndrey Smetanin }
5641f4b34f8SAndrey Smetanin 
5651f4b34f8SAndrey Smetanin static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
5667deec5e0SRoman Kagan 			     struct hv_message *src_msg, bool no_retry)
5671f4b34f8SAndrey Smetanin {
5681f4b34f8SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
5693a0e7731SRoman Kagan 	int msg_off = offsetof(struct hv_message_page, sint_message[sint]);
5703a0e7731SRoman Kagan 	gfn_t msg_page_gfn;
5713a0e7731SRoman Kagan 	struct hv_message_header hv_hdr;
5721f4b34f8SAndrey Smetanin 	int r;
5731f4b34f8SAndrey Smetanin 
5741f4b34f8SAndrey Smetanin 	if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
5751f4b34f8SAndrey Smetanin 		return -ENOENT;
5761f4b34f8SAndrey Smetanin 
5773a0e7731SRoman Kagan 	msg_page_gfn = synic->msg_page >> PAGE_SHIFT;
5781f4b34f8SAndrey Smetanin 
5793a0e7731SRoman Kagan 	/*
5803a0e7731SRoman Kagan 	 * Strictly following the spec-mandated ordering would assume setting
5813a0e7731SRoman Kagan 	 * .msg_pending before checking .message_type.  However, this function
5823a0e7731SRoman Kagan 	 * is only called in vcpu context so the entire update is atomic from
5833a0e7731SRoman Kagan 	 * guest POV and thus the exact order here doesn't matter.
5843a0e7731SRoman Kagan 	 */
5853a0e7731SRoman Kagan 	r = kvm_vcpu_read_guest_page(vcpu, msg_page_gfn, &hv_hdr.message_type,
5863a0e7731SRoman Kagan 				     msg_off + offsetof(struct hv_message,
5873a0e7731SRoman Kagan 							header.message_type),
5883a0e7731SRoman Kagan 				     sizeof(hv_hdr.message_type));
5893a0e7731SRoman Kagan 	if (r < 0)
5901f4b34f8SAndrey Smetanin 		return r;
5913a0e7731SRoman Kagan 
5923a0e7731SRoman Kagan 	if (hv_hdr.message_type != HVMSG_NONE) {
5937deec5e0SRoman Kagan 		if (no_retry)
5947deec5e0SRoman Kagan 			return 0;
5957deec5e0SRoman Kagan 
5963a0e7731SRoman Kagan 		hv_hdr.message_flags.msg_pending = 1;
5973a0e7731SRoman Kagan 		r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn,
5983a0e7731SRoman Kagan 					      &hv_hdr.message_flags,
5993a0e7731SRoman Kagan 					      msg_off +
6003a0e7731SRoman Kagan 					      offsetof(struct hv_message,
6013a0e7731SRoman Kagan 						       header.message_flags),
6023a0e7731SRoman Kagan 					      sizeof(hv_hdr.message_flags));
6033a0e7731SRoman Kagan 		if (r < 0)
6043a0e7731SRoman Kagan 			return r;
6053a0e7731SRoman Kagan 		return -EAGAIN;
6063a0e7731SRoman Kagan 	}
6073a0e7731SRoman Kagan 
6083a0e7731SRoman Kagan 	r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn, src_msg, msg_off,
6093a0e7731SRoman Kagan 				      sizeof(src_msg->header) +
6103a0e7731SRoman Kagan 				      src_msg->header.payload_size);
6113a0e7731SRoman Kagan 	if (r < 0)
6123a0e7731SRoman Kagan 		return r;
6133a0e7731SRoman Kagan 
6143a0e7731SRoman Kagan 	r = synic_set_irq(synic, sint);
6153a0e7731SRoman Kagan 	if (r < 0)
6163a0e7731SRoman Kagan 		return r;
6173a0e7731SRoman Kagan 	if (r == 0)
6183a0e7731SRoman Kagan 		return -EFAULT;
6193a0e7731SRoman Kagan 	return 0;
6201f4b34f8SAndrey Smetanin }
6211f4b34f8SAndrey Smetanin 
6220cdeabb1SAndrey Smetanin static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
6231f4b34f8SAndrey Smetanin {
6241f4b34f8SAndrey Smetanin 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
6251f4b34f8SAndrey Smetanin 	struct hv_message *msg = &stimer->msg;
6261f4b34f8SAndrey Smetanin 	struct hv_timer_message_payload *payload =
6271f4b34f8SAndrey Smetanin 			(struct hv_timer_message_payload *)&msg->u.payload;
6281f4b34f8SAndrey Smetanin 
6297deec5e0SRoman Kagan 	/*
6307deec5e0SRoman Kagan 	 * To avoid piling up periodic ticks, don't retry message
6317deec5e0SRoman Kagan 	 * delivery for them (within "lazy" lost ticks policy).
6327deec5e0SRoman Kagan 	 */
6336a058a1eSVitaly Kuznetsov 	bool no_retry = stimer->config.periodic;
6347deec5e0SRoman Kagan 
6351f4b34f8SAndrey Smetanin 	payload->expiration_time = stimer->exp_time;
6361f4b34f8SAndrey Smetanin 	payload->delivery_time = get_time_ref_counter(vcpu->kvm);
6370cdeabb1SAndrey Smetanin 	return synic_deliver_msg(vcpu_to_synic(vcpu),
6386a058a1eSVitaly Kuznetsov 				 stimer->config.sintx, msg,
6397deec5e0SRoman Kagan 				 no_retry);
6401f4b34f8SAndrey Smetanin }
6411f4b34f8SAndrey Smetanin 
6428644f771SVitaly Kuznetsov static int stimer_notify_direct(struct kvm_vcpu_hv_stimer *stimer)
6438644f771SVitaly Kuznetsov {
6448644f771SVitaly Kuznetsov 	struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
6458644f771SVitaly Kuznetsov 	struct kvm_lapic_irq irq = {
6468644f771SVitaly Kuznetsov 		.delivery_mode = APIC_DM_FIXED,
6478644f771SVitaly Kuznetsov 		.vector = stimer->config.apic_vector
6488644f771SVitaly Kuznetsov 	};
6498644f771SVitaly Kuznetsov 
650a073d7e3SWanpeng Li 	if (lapic_in_kernel(vcpu))
6518644f771SVitaly Kuznetsov 		return !kvm_apic_set_irq(vcpu, &irq, NULL);
652a073d7e3SWanpeng Li 	return 0;
6538644f771SVitaly Kuznetsov }
6548644f771SVitaly Kuznetsov 
6551f4b34f8SAndrey Smetanin static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
6561f4b34f8SAndrey Smetanin {
6578644f771SVitaly Kuznetsov 	int r, direct = stimer->config.direct_mode;
658ac3e5fcaSAndrey Smetanin 
6590cdeabb1SAndrey Smetanin 	stimer->msg_pending = true;
6608644f771SVitaly Kuznetsov 	if (!direct)
661ac3e5fcaSAndrey Smetanin 		r = stimer_send_msg(stimer);
6628644f771SVitaly Kuznetsov 	else
6638644f771SVitaly Kuznetsov 		r = stimer_notify_direct(stimer);
664ac3e5fcaSAndrey Smetanin 	trace_kvm_hv_stimer_expiration(stimer_to_vcpu(stimer)->vcpu_id,
6658644f771SVitaly Kuznetsov 				       stimer->index, direct, r);
666ac3e5fcaSAndrey Smetanin 	if (!r) {
6670cdeabb1SAndrey Smetanin 		stimer->msg_pending = false;
6686a058a1eSVitaly Kuznetsov 		if (!(stimer->config.periodic))
6696a058a1eSVitaly Kuznetsov 			stimer->config.enable = 0;
6700cdeabb1SAndrey Smetanin 	}
6711f4b34f8SAndrey Smetanin }
6721f4b34f8SAndrey Smetanin 
6731f4b34f8SAndrey Smetanin void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
6741f4b34f8SAndrey Smetanin {
6751f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
6761f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv_stimer *stimer;
677f3b138c5SAndrey Smetanin 	u64 time_now, exp_time;
6781f4b34f8SAndrey Smetanin 	int i;
6791f4b34f8SAndrey Smetanin 
6801f4b34f8SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
6811f4b34f8SAndrey Smetanin 		if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
6821f4b34f8SAndrey Smetanin 			stimer = &hv_vcpu->stimer[i];
6836a058a1eSVitaly Kuznetsov 			if (stimer->config.enable) {
684f3b138c5SAndrey Smetanin 				exp_time = stimer->exp_time;
6850cdeabb1SAndrey Smetanin 
686f3b138c5SAndrey Smetanin 				if (exp_time) {
687f3b138c5SAndrey Smetanin 					time_now =
688f3b138c5SAndrey Smetanin 						get_time_ref_counter(vcpu->kvm);
689f3b138c5SAndrey Smetanin 					if (time_now >= exp_time)
690f3b138c5SAndrey Smetanin 						stimer_expiration(stimer);
691f3b138c5SAndrey Smetanin 				}
692f3b138c5SAndrey Smetanin 
6936a058a1eSVitaly Kuznetsov 				if ((stimer->config.enable) &&
694f1ff89ecSRoman Kagan 				    stimer->count) {
695f1ff89ecSRoman Kagan 					if (!stimer->msg_pending)
6960cdeabb1SAndrey Smetanin 						stimer_start(stimer);
697f1ff89ecSRoman Kagan 				} else
6980cdeabb1SAndrey Smetanin 					stimer_cleanup(stimer);
6991f4b34f8SAndrey Smetanin 			}
7001f4b34f8SAndrey Smetanin 		}
7011f4b34f8SAndrey Smetanin }
7021f4b34f8SAndrey Smetanin 
7031f4b34f8SAndrey Smetanin void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)
7041f4b34f8SAndrey Smetanin {
7051f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
7061f4b34f8SAndrey Smetanin 	int i;
7071f4b34f8SAndrey Smetanin 
7081f4b34f8SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
7091f4b34f8SAndrey Smetanin 		stimer_cleanup(&hv_vcpu->stimer[i]);
7101f4b34f8SAndrey Smetanin }
7111f4b34f8SAndrey Smetanin 
71272bbf935SLadi Prosek bool kvm_hv_assist_page_enabled(struct kvm_vcpu *vcpu)
71372bbf935SLadi Prosek {
71472bbf935SLadi Prosek 	if (!(vcpu->arch.hyperv.hv_vapic & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE))
71572bbf935SLadi Prosek 		return false;
71672bbf935SLadi Prosek 	return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
71772bbf935SLadi Prosek }
71872bbf935SLadi Prosek EXPORT_SYMBOL_GPL(kvm_hv_assist_page_enabled);
71972bbf935SLadi Prosek 
72072bbf935SLadi Prosek bool kvm_hv_get_assist_page(struct kvm_vcpu *vcpu,
72172bbf935SLadi Prosek 			    struct hv_vp_assist_page *assist_page)
72272bbf935SLadi Prosek {
72372bbf935SLadi Prosek 	if (!kvm_hv_assist_page_enabled(vcpu))
72472bbf935SLadi Prosek 		return false;
72572bbf935SLadi Prosek 	return !kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data,
72672bbf935SLadi Prosek 				      assist_page, sizeof(*assist_page));
72772bbf935SLadi Prosek }
72872bbf935SLadi Prosek EXPORT_SYMBOL_GPL(kvm_hv_get_assist_page);
72972bbf935SLadi Prosek 
7301f4b34f8SAndrey Smetanin static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer)
7311f4b34f8SAndrey Smetanin {
7321f4b34f8SAndrey Smetanin 	struct hv_message *msg = &stimer->msg;
7331f4b34f8SAndrey Smetanin 	struct hv_timer_message_payload *payload =
7341f4b34f8SAndrey Smetanin 			(struct hv_timer_message_payload *)&msg->u.payload;
7351f4b34f8SAndrey Smetanin 
7361f4b34f8SAndrey Smetanin 	memset(&msg->header, 0, sizeof(msg->header));
7371f4b34f8SAndrey Smetanin 	msg->header.message_type = HVMSG_TIMER_EXPIRED;
7381f4b34f8SAndrey Smetanin 	msg->header.payload_size = sizeof(*payload);
7391f4b34f8SAndrey Smetanin 
7401f4b34f8SAndrey Smetanin 	payload->timer_index = stimer->index;
7411f4b34f8SAndrey Smetanin 	payload->expiration_time = 0;
7421f4b34f8SAndrey Smetanin 	payload->delivery_time = 0;
7431f4b34f8SAndrey Smetanin }
7441f4b34f8SAndrey Smetanin 
7451f4b34f8SAndrey Smetanin static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index)
7461f4b34f8SAndrey Smetanin {
7471f4b34f8SAndrey Smetanin 	memset(stimer, 0, sizeof(*stimer));
7481f4b34f8SAndrey Smetanin 	stimer->index = timer_index;
7491f4b34f8SAndrey Smetanin 	hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
7501f4b34f8SAndrey Smetanin 	stimer->timer.function = stimer_timer_callback;
7511f4b34f8SAndrey Smetanin 	stimer_prepare_msg(stimer);
7521f4b34f8SAndrey Smetanin }
7531f4b34f8SAndrey Smetanin 
7545c919412SAndrey Smetanin void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
7555c919412SAndrey Smetanin {
7561f4b34f8SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
7571f4b34f8SAndrey Smetanin 	int i;
7581f4b34f8SAndrey Smetanin 
7591f4b34f8SAndrey Smetanin 	synic_init(&hv_vcpu->synic);
7601f4b34f8SAndrey Smetanin 
7611f4b34f8SAndrey Smetanin 	bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
7621f4b34f8SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
7631f4b34f8SAndrey Smetanin 		stimer_init(&hv_vcpu->stimer[i], i);
7645c919412SAndrey Smetanin }
7655c919412SAndrey Smetanin 
766d3457c87SRoman Kagan void kvm_hv_vcpu_postcreate(struct kvm_vcpu *vcpu)
767d3457c87SRoman Kagan {
768d3457c87SRoman Kagan 	struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
769d3457c87SRoman Kagan 
770d3457c87SRoman Kagan 	hv_vcpu->vp_index = kvm_vcpu_get_idx(vcpu);
771d3457c87SRoman Kagan }
772d3457c87SRoman Kagan 
773efc479e6SRoman Kagan int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages)
7745c919412SAndrey Smetanin {
775efc479e6SRoman Kagan 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
776efc479e6SRoman Kagan 
7775c919412SAndrey Smetanin 	/*
7785c919412SAndrey Smetanin 	 * Hyper-V SynIC auto EOI SINT's are
779*f4fdc0a2SSuravee Suthikulpanit 	 * not compatible with APICV, so request
780*f4fdc0a2SSuravee Suthikulpanit 	 * to deactivate APICV permanently.
7815c919412SAndrey Smetanin 	 */
782*f4fdc0a2SSuravee Suthikulpanit 	kvm_request_apicv_update(vcpu->kvm, false, APICV_INHIBIT_REASON_HYPERV);
783efc479e6SRoman Kagan 	synic->active = true;
784efc479e6SRoman Kagan 	synic->dont_zero_synic_pages = dont_zero_synic_pages;
7855c919412SAndrey Smetanin 	return 0;
7865c919412SAndrey Smetanin }
7875c919412SAndrey Smetanin 
788e83d5887SAndrey Smetanin static bool kvm_hv_msr_partition_wide(u32 msr)
789e83d5887SAndrey Smetanin {
790e83d5887SAndrey Smetanin 	bool r = false;
791e83d5887SAndrey Smetanin 
792e83d5887SAndrey Smetanin 	switch (msr) {
793e83d5887SAndrey Smetanin 	case HV_X64_MSR_GUEST_OS_ID:
794e83d5887SAndrey Smetanin 	case HV_X64_MSR_HYPERCALL:
795e83d5887SAndrey Smetanin 	case HV_X64_MSR_REFERENCE_TSC:
796e83d5887SAndrey Smetanin 	case HV_X64_MSR_TIME_REF_COUNT:
797e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_CTL:
798e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
799e516cebbSAndrey Smetanin 	case HV_X64_MSR_RESET:
800a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
801a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_TSC_EMULATION_CONTROL:
802a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_TSC_EMULATION_STATUS:
803e83d5887SAndrey Smetanin 		r = true;
804e83d5887SAndrey Smetanin 		break;
805e83d5887SAndrey Smetanin 	}
806e83d5887SAndrey Smetanin 
807e83d5887SAndrey Smetanin 	return r;
808e83d5887SAndrey Smetanin }
809e83d5887SAndrey Smetanin 
810e7d9513bSAndrey Smetanin static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
811e7d9513bSAndrey Smetanin 				     u32 index, u64 *pdata)
812e7d9513bSAndrey Smetanin {
813e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
81486187937SMarios Pomonis 	size_t size = ARRAY_SIZE(hv->hv_crash_param);
815e7d9513bSAndrey Smetanin 
81686187937SMarios Pomonis 	if (WARN_ON_ONCE(index >= size))
817e7d9513bSAndrey Smetanin 		return -EINVAL;
818e7d9513bSAndrey Smetanin 
81986187937SMarios Pomonis 	*pdata = hv->hv_crash_param[array_index_nospec(index, size)];
820e7d9513bSAndrey Smetanin 	return 0;
821e7d9513bSAndrey Smetanin }
822e7d9513bSAndrey Smetanin 
823e7d9513bSAndrey Smetanin static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata)
824e7d9513bSAndrey Smetanin {
825e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
826e7d9513bSAndrey Smetanin 
827e7d9513bSAndrey Smetanin 	*pdata = hv->hv_crash_ctl;
828e7d9513bSAndrey Smetanin 	return 0;
829e7d9513bSAndrey Smetanin }
830e7d9513bSAndrey Smetanin 
831e7d9513bSAndrey Smetanin static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host)
832e7d9513bSAndrey Smetanin {
833e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
834e7d9513bSAndrey Smetanin 
835e7d9513bSAndrey Smetanin 	if (host)
836a4987defSVitaly Kuznetsov 		hv->hv_crash_ctl = data & HV_CRASH_CTL_CRASH_NOTIFY;
837e7d9513bSAndrey Smetanin 
838a4987defSVitaly Kuznetsov 	if (!host && (data & HV_CRASH_CTL_CRASH_NOTIFY)) {
839e7d9513bSAndrey Smetanin 
840e7d9513bSAndrey Smetanin 		vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
841e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[0],
842e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[1],
843e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[2],
844e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[3],
845e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[4]);
846e7d9513bSAndrey Smetanin 
847e7d9513bSAndrey Smetanin 		/* Send notification about crash to user space */
848e7d9513bSAndrey Smetanin 		kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
849e7d9513bSAndrey Smetanin 	}
850e7d9513bSAndrey Smetanin 
851e7d9513bSAndrey Smetanin 	return 0;
852e7d9513bSAndrey Smetanin }
853e7d9513bSAndrey Smetanin 
854e7d9513bSAndrey Smetanin static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
855e7d9513bSAndrey Smetanin 				     u32 index, u64 data)
856e7d9513bSAndrey Smetanin {
857e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
85886187937SMarios Pomonis 	size_t size = ARRAY_SIZE(hv->hv_crash_param);
859e7d9513bSAndrey Smetanin 
86086187937SMarios Pomonis 	if (WARN_ON_ONCE(index >= size))
861e7d9513bSAndrey Smetanin 		return -EINVAL;
862e7d9513bSAndrey Smetanin 
86386187937SMarios Pomonis 	hv->hv_crash_param[array_index_nospec(index, size)] = data;
864e7d9513bSAndrey Smetanin 	return 0;
865e7d9513bSAndrey Smetanin }
866e7d9513bSAndrey Smetanin 
867095cf55dSPaolo Bonzini /*
868095cf55dSPaolo Bonzini  * The kvmclock and Hyper-V TSC page use similar formulas, and converting
869095cf55dSPaolo Bonzini  * between them is possible:
870095cf55dSPaolo Bonzini  *
871095cf55dSPaolo Bonzini  * kvmclock formula:
872095cf55dSPaolo Bonzini  *    nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32)
873095cf55dSPaolo Bonzini  *           + system_time
874095cf55dSPaolo Bonzini  *
875095cf55dSPaolo Bonzini  * Hyper-V formula:
876095cf55dSPaolo Bonzini  *    nsec/100 = ticks * scale / 2^64 + offset
877095cf55dSPaolo Bonzini  *
878095cf55dSPaolo Bonzini  * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula.
879095cf55dSPaolo Bonzini  * By dividing the kvmclock formula by 100 and equating what's left we get:
880095cf55dSPaolo Bonzini  *    ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
881095cf55dSPaolo Bonzini  *            scale / 2^64 =         tsc_to_system_mul * 2^(tsc_shift-32) / 100
882095cf55dSPaolo Bonzini  *            scale        =         tsc_to_system_mul * 2^(32+tsc_shift) / 100
883095cf55dSPaolo Bonzini  *
884095cf55dSPaolo Bonzini  * Now expand the kvmclock formula and divide by 100:
885095cf55dSPaolo Bonzini  *    nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32)
886095cf55dSPaolo Bonzini  *           - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32)
887095cf55dSPaolo Bonzini  *           + system_time
888095cf55dSPaolo Bonzini  *    nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
889095cf55dSPaolo Bonzini  *               - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100
890095cf55dSPaolo Bonzini  *               + system_time / 100
891095cf55dSPaolo Bonzini  *
892095cf55dSPaolo Bonzini  * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64:
893095cf55dSPaolo Bonzini  *    nsec/100 = ticks * scale / 2^64
894095cf55dSPaolo Bonzini  *               - tsc_timestamp * scale / 2^64
895095cf55dSPaolo Bonzini  *               + system_time / 100
896095cf55dSPaolo Bonzini  *
897095cf55dSPaolo Bonzini  * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out:
898095cf55dSPaolo Bonzini  *    offset = system_time / 100 - tsc_timestamp * scale / 2^64
899095cf55dSPaolo Bonzini  *
900095cf55dSPaolo Bonzini  * These two equivalencies are implemented in this function.
901095cf55dSPaolo Bonzini  */
902095cf55dSPaolo Bonzini static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock,
903095cf55dSPaolo Bonzini 					HV_REFERENCE_TSC_PAGE *tsc_ref)
904095cf55dSPaolo Bonzini {
905095cf55dSPaolo Bonzini 	u64 max_mul;
906095cf55dSPaolo Bonzini 
907095cf55dSPaolo Bonzini 	if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT))
908095cf55dSPaolo Bonzini 		return false;
909095cf55dSPaolo Bonzini 
910095cf55dSPaolo Bonzini 	/*
911095cf55dSPaolo Bonzini 	 * check if scale would overflow, if so we use the time ref counter
912095cf55dSPaolo Bonzini 	 *    tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64
913095cf55dSPaolo Bonzini 	 *    tsc_to_system_mul / 100 >= 2^(32-tsc_shift)
914095cf55dSPaolo Bonzini 	 *    tsc_to_system_mul >= 100 * 2^(32-tsc_shift)
915095cf55dSPaolo Bonzini 	 */
916095cf55dSPaolo Bonzini 	max_mul = 100ull << (32 - hv_clock->tsc_shift);
917095cf55dSPaolo Bonzini 	if (hv_clock->tsc_to_system_mul >= max_mul)
918095cf55dSPaolo Bonzini 		return false;
919095cf55dSPaolo Bonzini 
920095cf55dSPaolo Bonzini 	/*
921095cf55dSPaolo Bonzini 	 * Otherwise compute the scale and offset according to the formulas
922095cf55dSPaolo Bonzini 	 * derived above.
923095cf55dSPaolo Bonzini 	 */
924095cf55dSPaolo Bonzini 	tsc_ref->tsc_scale =
925095cf55dSPaolo Bonzini 		mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift),
926095cf55dSPaolo Bonzini 				hv_clock->tsc_to_system_mul,
927095cf55dSPaolo Bonzini 				100);
928095cf55dSPaolo Bonzini 
929095cf55dSPaolo Bonzini 	tsc_ref->tsc_offset = hv_clock->system_time;
930095cf55dSPaolo Bonzini 	do_div(tsc_ref->tsc_offset, 100);
931095cf55dSPaolo Bonzini 	tsc_ref->tsc_offset -=
932095cf55dSPaolo Bonzini 		mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64);
933095cf55dSPaolo Bonzini 	return true;
934095cf55dSPaolo Bonzini }
935095cf55dSPaolo Bonzini 
936095cf55dSPaolo Bonzini void kvm_hv_setup_tsc_page(struct kvm *kvm,
937095cf55dSPaolo Bonzini 			   struct pvclock_vcpu_time_info *hv_clock)
938095cf55dSPaolo Bonzini {
939095cf55dSPaolo Bonzini 	struct kvm_hv *hv = &kvm->arch.hyperv;
940095cf55dSPaolo Bonzini 	u32 tsc_seq;
941095cf55dSPaolo Bonzini 	u64 gfn;
942095cf55dSPaolo Bonzini 
943095cf55dSPaolo Bonzini 	BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence));
944095cf55dSPaolo Bonzini 	BUILD_BUG_ON(offsetof(HV_REFERENCE_TSC_PAGE, tsc_sequence) != 0);
945095cf55dSPaolo Bonzini 
946095cf55dSPaolo Bonzini 	if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
947095cf55dSPaolo Bonzini 		return;
948095cf55dSPaolo Bonzini 
9493f5ad8beSPaolo Bonzini 	mutex_lock(&kvm->arch.hyperv.hv_lock);
9503f5ad8beSPaolo Bonzini 	if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
9513f5ad8beSPaolo Bonzini 		goto out_unlock;
9523f5ad8beSPaolo Bonzini 
953095cf55dSPaolo Bonzini 	gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
954095cf55dSPaolo Bonzini 	/*
955095cf55dSPaolo Bonzini 	 * Because the TSC parameters only vary when there is a
956095cf55dSPaolo Bonzini 	 * change in the master clock, do not bother with caching.
957095cf55dSPaolo Bonzini 	 */
958095cf55dSPaolo Bonzini 	if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn),
959095cf55dSPaolo Bonzini 				    &tsc_seq, sizeof(tsc_seq))))
9603f5ad8beSPaolo Bonzini 		goto out_unlock;
961095cf55dSPaolo Bonzini 
962095cf55dSPaolo Bonzini 	/*
963095cf55dSPaolo Bonzini 	 * While we're computing and writing the parameters, force the
964095cf55dSPaolo Bonzini 	 * guest to use the time reference count MSR.
965095cf55dSPaolo Bonzini 	 */
966095cf55dSPaolo Bonzini 	hv->tsc_ref.tsc_sequence = 0;
967095cf55dSPaolo Bonzini 	if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
968095cf55dSPaolo Bonzini 			    &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
9693f5ad8beSPaolo Bonzini 		goto out_unlock;
970095cf55dSPaolo Bonzini 
971095cf55dSPaolo Bonzini 	if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref))
9723f5ad8beSPaolo Bonzini 		goto out_unlock;
973095cf55dSPaolo Bonzini 
974095cf55dSPaolo Bonzini 	/* Ensure sequence is zero before writing the rest of the struct.  */
975095cf55dSPaolo Bonzini 	smp_wmb();
976095cf55dSPaolo Bonzini 	if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
9773f5ad8beSPaolo Bonzini 		goto out_unlock;
978095cf55dSPaolo Bonzini 
979095cf55dSPaolo Bonzini 	/*
980095cf55dSPaolo Bonzini 	 * Now switch to the TSC page mechanism by writing the sequence.
981095cf55dSPaolo Bonzini 	 */
982095cf55dSPaolo Bonzini 	tsc_seq++;
983095cf55dSPaolo Bonzini 	if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0)
984095cf55dSPaolo Bonzini 		tsc_seq = 1;
985095cf55dSPaolo Bonzini 
986095cf55dSPaolo Bonzini 	/* Write the struct entirely before the non-zero sequence.  */
987095cf55dSPaolo Bonzini 	smp_wmb();
988095cf55dSPaolo Bonzini 
989095cf55dSPaolo Bonzini 	hv->tsc_ref.tsc_sequence = tsc_seq;
990095cf55dSPaolo Bonzini 	kvm_write_guest(kvm, gfn_to_gpa(gfn),
991095cf55dSPaolo Bonzini 			&hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence));
9923f5ad8beSPaolo Bonzini out_unlock:
9933f5ad8beSPaolo Bonzini 	mutex_unlock(&kvm->arch.hyperv.hv_lock);
994095cf55dSPaolo Bonzini }
995095cf55dSPaolo Bonzini 
996e7d9513bSAndrey Smetanin static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
997e7d9513bSAndrey Smetanin 			     bool host)
998e83d5887SAndrey Smetanin {
999e83d5887SAndrey Smetanin 	struct kvm *kvm = vcpu->kvm;
1000e83d5887SAndrey Smetanin 	struct kvm_hv *hv = &kvm->arch.hyperv;
1001e83d5887SAndrey Smetanin 
1002e83d5887SAndrey Smetanin 	switch (msr) {
1003e83d5887SAndrey Smetanin 	case HV_X64_MSR_GUEST_OS_ID:
1004e83d5887SAndrey Smetanin 		hv->hv_guest_os_id = data;
1005e83d5887SAndrey Smetanin 		/* setting guest os id to zero disables hypercall page */
1006e83d5887SAndrey Smetanin 		if (!hv->hv_guest_os_id)
1007e83d5887SAndrey Smetanin 			hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
1008e83d5887SAndrey Smetanin 		break;
1009e83d5887SAndrey Smetanin 	case HV_X64_MSR_HYPERCALL: {
1010e83d5887SAndrey Smetanin 		u64 gfn;
1011e83d5887SAndrey Smetanin 		unsigned long addr;
1012e83d5887SAndrey Smetanin 		u8 instructions[4];
1013e83d5887SAndrey Smetanin 
1014e83d5887SAndrey Smetanin 		/* if guest os id is not set hypercall should remain disabled */
1015e83d5887SAndrey Smetanin 		if (!hv->hv_guest_os_id)
1016e83d5887SAndrey Smetanin 			break;
1017e83d5887SAndrey Smetanin 		if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
1018e83d5887SAndrey Smetanin 			hv->hv_hypercall = data;
1019e83d5887SAndrey Smetanin 			break;
1020e83d5887SAndrey Smetanin 		}
1021e83d5887SAndrey Smetanin 		gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
1022e83d5887SAndrey Smetanin 		addr = gfn_to_hva(kvm, gfn);
1023e83d5887SAndrey Smetanin 		if (kvm_is_error_hva(addr))
1024e83d5887SAndrey Smetanin 			return 1;
1025e83d5887SAndrey Smetanin 		kvm_x86_ops->patch_hypercall(vcpu, instructions);
1026e83d5887SAndrey Smetanin 		((unsigned char *)instructions)[3] = 0xc3; /* ret */
1027e83d5887SAndrey Smetanin 		if (__copy_to_user((void __user *)addr, instructions, 4))
1028e83d5887SAndrey Smetanin 			return 1;
1029e83d5887SAndrey Smetanin 		hv->hv_hypercall = data;
1030e83d5887SAndrey Smetanin 		mark_page_dirty(kvm, gfn);
1031e83d5887SAndrey Smetanin 		break;
1032e83d5887SAndrey Smetanin 	}
1033095cf55dSPaolo Bonzini 	case HV_X64_MSR_REFERENCE_TSC:
1034e83d5887SAndrey Smetanin 		hv->hv_tsc_page = data;
1035095cf55dSPaolo Bonzini 		if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)
1036095cf55dSPaolo Bonzini 			kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1037e83d5887SAndrey Smetanin 		break;
1038e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1039e7d9513bSAndrey Smetanin 		return kvm_hv_msr_set_crash_data(vcpu,
1040e7d9513bSAndrey Smetanin 						 msr - HV_X64_MSR_CRASH_P0,
1041e7d9513bSAndrey Smetanin 						 data);
1042e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_CTL:
1043e7d9513bSAndrey Smetanin 		return kvm_hv_msr_set_crash_ctl(vcpu, data, host);
1044e516cebbSAndrey Smetanin 	case HV_X64_MSR_RESET:
1045e516cebbSAndrey Smetanin 		if (data == 1) {
1046e516cebbSAndrey Smetanin 			vcpu_debug(vcpu, "hyper-v reset requested\n");
1047e516cebbSAndrey Smetanin 			kvm_make_request(KVM_REQ_HV_RESET, vcpu);
1048e516cebbSAndrey Smetanin 		}
1049e516cebbSAndrey Smetanin 		break;
1050a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1051a2e164e7SVitaly Kuznetsov 		hv->hv_reenlightenment_control = data;
1052a2e164e7SVitaly Kuznetsov 		break;
1053a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_TSC_EMULATION_CONTROL:
1054a2e164e7SVitaly Kuznetsov 		hv->hv_tsc_emulation_control = data;
1055a2e164e7SVitaly Kuznetsov 		break;
1056a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_TSC_EMULATION_STATUS:
1057a2e164e7SVitaly Kuznetsov 		hv->hv_tsc_emulation_status = data;
1058a2e164e7SVitaly Kuznetsov 		break;
105944883f01SPaolo Bonzini 	case HV_X64_MSR_TIME_REF_COUNT:
106044883f01SPaolo Bonzini 		/* read-only, but still ignore it if host-initiated */
106144883f01SPaolo Bonzini 		if (!host)
106244883f01SPaolo Bonzini 			return 1;
106344883f01SPaolo Bonzini 		break;
1064e83d5887SAndrey Smetanin 	default:
10652f9f5cddSMiaohe Lin 		vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n",
1066e83d5887SAndrey Smetanin 			    msr, data);
1067e83d5887SAndrey Smetanin 		return 1;
1068e83d5887SAndrey Smetanin 	}
1069e83d5887SAndrey Smetanin 	return 0;
1070e83d5887SAndrey Smetanin }
1071e83d5887SAndrey Smetanin 
10729eec50b8SAndrey Smetanin /* Calculate cpu time spent by current task in 100ns units */
10739eec50b8SAndrey Smetanin static u64 current_task_runtime_100ns(void)
10749eec50b8SAndrey Smetanin {
10755613fda9SFrederic Weisbecker 	u64 utime, stime;
10769eec50b8SAndrey Smetanin 
10779eec50b8SAndrey Smetanin 	task_cputime_adjusted(current, &utime, &stime);
10785613fda9SFrederic Weisbecker 
10795613fda9SFrederic Weisbecker 	return div_u64(utime + stime, 100);
10809eec50b8SAndrey Smetanin }
10819eec50b8SAndrey Smetanin 
10829eec50b8SAndrey Smetanin static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
1083e83d5887SAndrey Smetanin {
10841779a39fSVitaly Kuznetsov 	struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
1085e83d5887SAndrey Smetanin 
1086e83d5887SAndrey Smetanin 	switch (msr) {
108787ee613dSVitaly Kuznetsov 	case HV_X64_MSR_VP_INDEX: {
108887ee613dSVitaly Kuznetsov 		struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
108987ee613dSVitaly Kuznetsov 		int vcpu_idx = kvm_vcpu_get_idx(vcpu);
109087ee613dSVitaly Kuznetsov 		u32 new_vp_index = (u32)data;
109187ee613dSVitaly Kuznetsov 
109287ee613dSVitaly Kuznetsov 		if (!host || new_vp_index >= KVM_MAX_VCPUS)
1093d3457c87SRoman Kagan 			return 1;
109487ee613dSVitaly Kuznetsov 
109587ee613dSVitaly Kuznetsov 		if (new_vp_index == hv_vcpu->vp_index)
109687ee613dSVitaly Kuznetsov 			return 0;
109787ee613dSVitaly Kuznetsov 
109887ee613dSVitaly Kuznetsov 		/*
109987ee613dSVitaly Kuznetsov 		 * The VP index is initialized to vcpu_index by
110087ee613dSVitaly Kuznetsov 		 * kvm_hv_vcpu_postcreate so they initially match.  Now the
110187ee613dSVitaly Kuznetsov 		 * VP index is changing, adjust num_mismatched_vp_indexes if
110287ee613dSVitaly Kuznetsov 		 * it now matches or no longer matches vcpu_idx.
110387ee613dSVitaly Kuznetsov 		 */
110487ee613dSVitaly Kuznetsov 		if (hv_vcpu->vp_index == vcpu_idx)
110587ee613dSVitaly Kuznetsov 			atomic_inc(&hv->num_mismatched_vp_indexes);
110687ee613dSVitaly Kuznetsov 		else if (new_vp_index == vcpu_idx)
110787ee613dSVitaly Kuznetsov 			atomic_dec(&hv->num_mismatched_vp_indexes);
110887ee613dSVitaly Kuznetsov 
110987ee613dSVitaly Kuznetsov 		hv_vcpu->vp_index = new_vp_index;
1110d3457c87SRoman Kagan 		break;
111187ee613dSVitaly Kuznetsov 	}
1112d4abc577SLadi Prosek 	case HV_X64_MSR_VP_ASSIST_PAGE: {
1113e83d5887SAndrey Smetanin 		u64 gfn;
1114e83d5887SAndrey Smetanin 		unsigned long addr;
1115e83d5887SAndrey Smetanin 
1116d4abc577SLadi Prosek 		if (!(data & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE)) {
11171779a39fSVitaly Kuznetsov 			hv_vcpu->hv_vapic = data;
111872bbf935SLadi Prosek 			if (kvm_lapic_enable_pv_eoi(vcpu, 0, 0))
1119e83d5887SAndrey Smetanin 				return 1;
1120e83d5887SAndrey Smetanin 			break;
1121e83d5887SAndrey Smetanin 		}
1122d4abc577SLadi Prosek 		gfn = data >> HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT;
1123e83d5887SAndrey Smetanin 		addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
1124e83d5887SAndrey Smetanin 		if (kvm_is_error_hva(addr))
1125e83d5887SAndrey Smetanin 			return 1;
112612e0c618SVitaly Kuznetsov 
112712e0c618SVitaly Kuznetsov 		/*
112867b0ae43SMiaohe Lin 		 * Clear apic_assist portion of struct hv_vp_assist_page
112912e0c618SVitaly Kuznetsov 		 * only, there can be valuable data in the rest which needs
113012e0c618SVitaly Kuznetsov 		 * to be preserved e.g. on migration.
113112e0c618SVitaly Kuznetsov 		 */
113212e0c618SVitaly Kuznetsov 		if (__clear_user((void __user *)addr, sizeof(u32)))
1133e83d5887SAndrey Smetanin 			return 1;
11341779a39fSVitaly Kuznetsov 		hv_vcpu->hv_vapic = data;
1135e83d5887SAndrey Smetanin 		kvm_vcpu_mark_page_dirty(vcpu, gfn);
1136e83d5887SAndrey Smetanin 		if (kvm_lapic_enable_pv_eoi(vcpu,
113772bbf935SLadi Prosek 					    gfn_to_gpa(gfn) | KVM_MSR_ENABLED,
113872bbf935SLadi Prosek 					    sizeof(struct hv_vp_assist_page)))
1139e83d5887SAndrey Smetanin 			return 1;
1140e83d5887SAndrey Smetanin 		break;
1141e83d5887SAndrey Smetanin 	}
1142e83d5887SAndrey Smetanin 	case HV_X64_MSR_EOI:
1143e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1144e83d5887SAndrey Smetanin 	case HV_X64_MSR_ICR:
1145e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1146e83d5887SAndrey Smetanin 	case HV_X64_MSR_TPR:
1147e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
11489eec50b8SAndrey Smetanin 	case HV_X64_MSR_VP_RUNTIME:
11499eec50b8SAndrey Smetanin 		if (!host)
11509eec50b8SAndrey Smetanin 			return 1;
11511779a39fSVitaly Kuznetsov 		hv_vcpu->runtime_offset = data - current_task_runtime_100ns();
11529eec50b8SAndrey Smetanin 		break;
11535c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
11545c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
11555c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
11565c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
11575c919412SAndrey Smetanin 	case HV_X64_MSR_EOM:
11585c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
11595c919412SAndrey Smetanin 		return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host);
11601f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER0_CONFIG:
11611f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER1_CONFIG:
11621f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER2_CONFIG:
11631f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER3_CONFIG: {
11641f4b34f8SAndrey Smetanin 		int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
11651f4b34f8SAndrey Smetanin 
11661f4b34f8SAndrey Smetanin 		return stimer_set_config(vcpu_to_stimer(vcpu, timer_index),
11671f4b34f8SAndrey Smetanin 					 data, host);
11681f4b34f8SAndrey Smetanin 	}
11691f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER0_COUNT:
11701f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER1_COUNT:
11711f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER2_COUNT:
11721f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER3_COUNT: {
11731f4b34f8SAndrey Smetanin 		int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
11741f4b34f8SAndrey Smetanin 
11751f4b34f8SAndrey Smetanin 		return stimer_set_count(vcpu_to_stimer(vcpu, timer_index),
11761f4b34f8SAndrey Smetanin 					data, host);
11771f4b34f8SAndrey Smetanin 	}
117844883f01SPaolo Bonzini 	case HV_X64_MSR_TSC_FREQUENCY:
117944883f01SPaolo Bonzini 	case HV_X64_MSR_APIC_FREQUENCY:
118044883f01SPaolo Bonzini 		/* read-only, but still ignore it if host-initiated */
118144883f01SPaolo Bonzini 		if (!host)
118244883f01SPaolo Bonzini 			return 1;
118344883f01SPaolo Bonzini 		break;
1184e83d5887SAndrey Smetanin 	default:
11852f9f5cddSMiaohe Lin 		vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n",
1186e83d5887SAndrey Smetanin 			    msr, data);
1187e83d5887SAndrey Smetanin 		return 1;
1188e83d5887SAndrey Smetanin 	}
1189e83d5887SAndrey Smetanin 
1190e83d5887SAndrey Smetanin 	return 0;
1191e83d5887SAndrey Smetanin }
1192e83d5887SAndrey Smetanin 
1193e83d5887SAndrey Smetanin static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1194e83d5887SAndrey Smetanin {
1195e83d5887SAndrey Smetanin 	u64 data = 0;
1196e83d5887SAndrey Smetanin 	struct kvm *kvm = vcpu->kvm;
1197e83d5887SAndrey Smetanin 	struct kvm_hv *hv = &kvm->arch.hyperv;
1198e83d5887SAndrey Smetanin 
1199e83d5887SAndrey Smetanin 	switch (msr) {
1200e83d5887SAndrey Smetanin 	case HV_X64_MSR_GUEST_OS_ID:
1201e83d5887SAndrey Smetanin 		data = hv->hv_guest_os_id;
1202e83d5887SAndrey Smetanin 		break;
1203e83d5887SAndrey Smetanin 	case HV_X64_MSR_HYPERCALL:
1204e83d5887SAndrey Smetanin 		data = hv->hv_hypercall;
1205e83d5887SAndrey Smetanin 		break;
120693bf4172SAndrey Smetanin 	case HV_X64_MSR_TIME_REF_COUNT:
120793bf4172SAndrey Smetanin 		data = get_time_ref_counter(kvm);
1208e83d5887SAndrey Smetanin 		break;
1209e83d5887SAndrey Smetanin 	case HV_X64_MSR_REFERENCE_TSC:
1210e83d5887SAndrey Smetanin 		data = hv->hv_tsc_page;
1211e83d5887SAndrey Smetanin 		break;
1212e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1213e7d9513bSAndrey Smetanin 		return kvm_hv_msr_get_crash_data(vcpu,
1214e7d9513bSAndrey Smetanin 						 msr - HV_X64_MSR_CRASH_P0,
1215e7d9513bSAndrey Smetanin 						 pdata);
1216e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_CTL:
1217e7d9513bSAndrey Smetanin 		return kvm_hv_msr_get_crash_ctl(vcpu, pdata);
1218e516cebbSAndrey Smetanin 	case HV_X64_MSR_RESET:
1219e516cebbSAndrey Smetanin 		data = 0;
1220e516cebbSAndrey Smetanin 		break;
1221a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1222a2e164e7SVitaly Kuznetsov 		data = hv->hv_reenlightenment_control;
1223a2e164e7SVitaly Kuznetsov 		break;
1224a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_TSC_EMULATION_CONTROL:
1225a2e164e7SVitaly Kuznetsov 		data = hv->hv_tsc_emulation_control;
1226a2e164e7SVitaly Kuznetsov 		break;
1227a2e164e7SVitaly Kuznetsov 	case HV_X64_MSR_TSC_EMULATION_STATUS:
1228a2e164e7SVitaly Kuznetsov 		data = hv->hv_tsc_emulation_status;
1229a2e164e7SVitaly Kuznetsov 		break;
1230e83d5887SAndrey Smetanin 	default:
1231e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1232e83d5887SAndrey Smetanin 		return 1;
1233e83d5887SAndrey Smetanin 	}
1234e83d5887SAndrey Smetanin 
1235e83d5887SAndrey Smetanin 	*pdata = data;
1236e83d5887SAndrey Smetanin 	return 0;
1237e83d5887SAndrey Smetanin }
1238e83d5887SAndrey Smetanin 
123944883f01SPaolo Bonzini static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,
124044883f01SPaolo Bonzini 			  bool host)
1241e83d5887SAndrey Smetanin {
1242e83d5887SAndrey Smetanin 	u64 data = 0;
12431779a39fSVitaly Kuznetsov 	struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
1244e83d5887SAndrey Smetanin 
1245e83d5887SAndrey Smetanin 	switch (msr) {
1246d3457c87SRoman Kagan 	case HV_X64_MSR_VP_INDEX:
12471779a39fSVitaly Kuznetsov 		data = hv_vcpu->vp_index;
1248e83d5887SAndrey Smetanin 		break;
1249e83d5887SAndrey Smetanin 	case HV_X64_MSR_EOI:
1250e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1251e83d5887SAndrey Smetanin 	case HV_X64_MSR_ICR:
1252e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1253e83d5887SAndrey Smetanin 	case HV_X64_MSR_TPR:
1254e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
1255d4abc577SLadi Prosek 	case HV_X64_MSR_VP_ASSIST_PAGE:
12561779a39fSVitaly Kuznetsov 		data = hv_vcpu->hv_vapic;
1257e83d5887SAndrey Smetanin 		break;
12589eec50b8SAndrey Smetanin 	case HV_X64_MSR_VP_RUNTIME:
12591779a39fSVitaly Kuznetsov 		data = current_task_runtime_100ns() + hv_vcpu->runtime_offset;
12609eec50b8SAndrey Smetanin 		break;
12615c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
12625c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
12635c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
12645c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
12655c919412SAndrey Smetanin 	case HV_X64_MSR_EOM:
12665c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
126744883f01SPaolo Bonzini 		return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata, host);
12681f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER0_CONFIG:
12691f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER1_CONFIG:
12701f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER2_CONFIG:
12711f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER3_CONFIG: {
12721f4b34f8SAndrey Smetanin 		int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
12731f4b34f8SAndrey Smetanin 
12741f4b34f8SAndrey Smetanin 		return stimer_get_config(vcpu_to_stimer(vcpu, timer_index),
12751f4b34f8SAndrey Smetanin 					 pdata);
12761f4b34f8SAndrey Smetanin 	}
12771f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER0_COUNT:
12781f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER1_COUNT:
12791f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER2_COUNT:
12801f4b34f8SAndrey Smetanin 	case HV_X64_MSR_STIMER3_COUNT: {
12811f4b34f8SAndrey Smetanin 		int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
12821f4b34f8SAndrey Smetanin 
12831f4b34f8SAndrey Smetanin 		return stimer_get_count(vcpu_to_stimer(vcpu, timer_index),
12841f4b34f8SAndrey Smetanin 					pdata);
12851f4b34f8SAndrey Smetanin 	}
128672c139baSLadi Prosek 	case HV_X64_MSR_TSC_FREQUENCY:
128772c139baSLadi Prosek 		data = (u64)vcpu->arch.virtual_tsc_khz * 1000;
128872c139baSLadi Prosek 		break;
128972c139baSLadi Prosek 	case HV_X64_MSR_APIC_FREQUENCY:
129072c139baSLadi Prosek 		data = APIC_BUS_FREQUENCY;
129172c139baSLadi Prosek 		break;
1292e83d5887SAndrey Smetanin 	default:
1293e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1294e83d5887SAndrey Smetanin 		return 1;
1295e83d5887SAndrey Smetanin 	}
1296e83d5887SAndrey Smetanin 	*pdata = data;
1297e83d5887SAndrey Smetanin 	return 0;
1298e83d5887SAndrey Smetanin }
1299e83d5887SAndrey Smetanin 
1300e7d9513bSAndrey Smetanin int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
1301e83d5887SAndrey Smetanin {
1302e83d5887SAndrey Smetanin 	if (kvm_hv_msr_partition_wide(msr)) {
1303e83d5887SAndrey Smetanin 		int r;
1304e83d5887SAndrey Smetanin 
13053f5ad8beSPaolo Bonzini 		mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
1306e7d9513bSAndrey Smetanin 		r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
13073f5ad8beSPaolo Bonzini 		mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
1308e83d5887SAndrey Smetanin 		return r;
1309e83d5887SAndrey Smetanin 	} else
13109eec50b8SAndrey Smetanin 		return kvm_hv_set_msr(vcpu, msr, data, host);
1311e83d5887SAndrey Smetanin }
1312e83d5887SAndrey Smetanin 
131344883f01SPaolo Bonzini int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
1314e83d5887SAndrey Smetanin {
1315e83d5887SAndrey Smetanin 	if (kvm_hv_msr_partition_wide(msr)) {
1316e83d5887SAndrey Smetanin 		int r;
1317e83d5887SAndrey Smetanin 
13183f5ad8beSPaolo Bonzini 		mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
1319e83d5887SAndrey Smetanin 		r = kvm_hv_get_msr_pw(vcpu, msr, pdata);
13203f5ad8beSPaolo Bonzini 		mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
1321e83d5887SAndrey Smetanin 		return r;
1322e83d5887SAndrey Smetanin 	} else
132344883f01SPaolo Bonzini 		return kvm_hv_get_msr(vcpu, msr, pdata, host);
1324e83d5887SAndrey Smetanin }
1325e83d5887SAndrey Smetanin 
1326f21dd494SVitaly Kuznetsov static __always_inline unsigned long *sparse_set_to_vcpu_mask(
1327f21dd494SVitaly Kuznetsov 	struct kvm *kvm, u64 *sparse_banks, u64 valid_bank_mask,
1328f21dd494SVitaly Kuznetsov 	u64 *vp_bitmap, unsigned long *vcpu_bitmap)
1329c7012676SVitaly Kuznetsov {
1330f21dd494SVitaly Kuznetsov 	struct kvm_hv *hv = &kvm->arch.hyperv;
1331f21dd494SVitaly Kuznetsov 	struct kvm_vcpu *vcpu;
1332f21dd494SVitaly Kuznetsov 	int i, bank, sbank = 0;
1333c7012676SVitaly Kuznetsov 
1334f21dd494SVitaly Kuznetsov 	memset(vp_bitmap, 0,
1335f21dd494SVitaly Kuznetsov 	       KVM_HV_MAX_SPARSE_VCPU_SET_BITS * sizeof(*vp_bitmap));
1336f21dd494SVitaly Kuznetsov 	for_each_set_bit(bank, (unsigned long *)&valid_bank_mask,
1337f21dd494SVitaly Kuznetsov 			 KVM_HV_MAX_SPARSE_VCPU_SET_BITS)
1338f21dd494SVitaly Kuznetsov 		vp_bitmap[bank] = sparse_banks[sbank++];
1339c7012676SVitaly Kuznetsov 
1340f21dd494SVitaly Kuznetsov 	if (likely(!atomic_read(&hv->num_mismatched_vp_indexes))) {
1341f21dd494SVitaly Kuznetsov 		/* for all vcpus vp_index == vcpu_idx */
1342f21dd494SVitaly Kuznetsov 		return (unsigned long *)vp_bitmap;
1343f21dd494SVitaly Kuznetsov 	}
1344c7012676SVitaly Kuznetsov 
1345f21dd494SVitaly Kuznetsov 	bitmap_zero(vcpu_bitmap, KVM_MAX_VCPUS);
1346f21dd494SVitaly Kuznetsov 	kvm_for_each_vcpu(i, vcpu, kvm) {
1347f21dd494SVitaly Kuznetsov 		if (test_bit(vcpu_to_hv_vcpu(vcpu)->vp_index,
1348f21dd494SVitaly Kuznetsov 			     (unsigned long *)vp_bitmap))
1349f21dd494SVitaly Kuznetsov 			__set_bit(i, vcpu_bitmap);
1350f21dd494SVitaly Kuznetsov 	}
1351f21dd494SVitaly Kuznetsov 	return vcpu_bitmap;
1352c7012676SVitaly Kuznetsov }
1353c7012676SVitaly Kuznetsov 
1354e2f11f42SVitaly Kuznetsov static u64 kvm_hv_flush_tlb(struct kvm_vcpu *current_vcpu, u64 ingpa,
1355c7012676SVitaly Kuznetsov 			    u16 rep_cnt, bool ex)
1356e2f11f42SVitaly Kuznetsov {
1357e2f11f42SVitaly Kuznetsov 	struct kvm *kvm = current_vcpu->kvm;
13582cefc5feSVitaly Kuznetsov 	struct kvm_vcpu_hv *hv_vcpu = &current_vcpu->arch.hyperv;
1359c7012676SVitaly Kuznetsov 	struct hv_tlb_flush_ex flush_ex;
1360e2f11f42SVitaly Kuznetsov 	struct hv_tlb_flush flush;
1361f21dd494SVitaly Kuznetsov 	u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS];
1362f21dd494SVitaly Kuznetsov 	DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS);
1363f21dd494SVitaly Kuznetsov 	unsigned long *vcpu_mask;
13642cefc5feSVitaly Kuznetsov 	u64 valid_bank_mask;
1365c7012676SVitaly Kuznetsov 	u64 sparse_banks[64];
1366f21dd494SVitaly Kuznetsov 	int sparse_banks_len;
1367c7012676SVitaly Kuznetsov 	bool all_cpus;
1368e2f11f42SVitaly Kuznetsov 
1369c7012676SVitaly Kuznetsov 	if (!ex) {
1370e2f11f42SVitaly Kuznetsov 		if (unlikely(kvm_read_guest(kvm, ingpa, &flush, sizeof(flush))))
1371e2f11f42SVitaly Kuznetsov 			return HV_STATUS_INVALID_HYPERCALL_INPUT;
1372e2f11f42SVitaly Kuznetsov 
1373c7012676SVitaly Kuznetsov 		trace_kvm_hv_flush_tlb(flush.processor_mask,
1374c7012676SVitaly Kuznetsov 				       flush.address_space, flush.flags);
1375c7012676SVitaly Kuznetsov 
13762cefc5feSVitaly Kuznetsov 		valid_bank_mask = BIT_ULL(0);
1377c7012676SVitaly Kuznetsov 		sparse_banks[0] = flush.processor_mask;
1378da66761cSVitaly Kuznetsov 
1379da66761cSVitaly Kuznetsov 		/*
1380da66761cSVitaly Kuznetsov 		 * Work around possible WS2012 bug: it sends hypercalls
1381da66761cSVitaly Kuznetsov 		 * with processor_mask = 0x0 and HV_FLUSH_ALL_PROCESSORS clear,
1382da66761cSVitaly Kuznetsov 		 * while also expecting us to flush something and crashing if
1383da66761cSVitaly Kuznetsov 		 * we don't. Let's treat processor_mask == 0 same as
1384da66761cSVitaly Kuznetsov 		 * HV_FLUSH_ALL_PROCESSORS.
1385da66761cSVitaly Kuznetsov 		 */
1386da66761cSVitaly Kuznetsov 		all_cpus = (flush.flags & HV_FLUSH_ALL_PROCESSORS) ||
1387da66761cSVitaly Kuznetsov 			flush.processor_mask == 0;
1388c7012676SVitaly Kuznetsov 	} else {
1389c7012676SVitaly Kuznetsov 		if (unlikely(kvm_read_guest(kvm, ingpa, &flush_ex,
1390c7012676SVitaly Kuznetsov 					    sizeof(flush_ex))))
1391c7012676SVitaly Kuznetsov 			return HV_STATUS_INVALID_HYPERCALL_INPUT;
1392c7012676SVitaly Kuznetsov 
1393c7012676SVitaly Kuznetsov 		trace_kvm_hv_flush_tlb_ex(flush_ex.hv_vp_set.valid_bank_mask,
1394c7012676SVitaly Kuznetsov 					  flush_ex.hv_vp_set.format,
1395c7012676SVitaly Kuznetsov 					  flush_ex.address_space,
1396c7012676SVitaly Kuznetsov 					  flush_ex.flags);
1397c7012676SVitaly Kuznetsov 
1398c7012676SVitaly Kuznetsov 		valid_bank_mask = flush_ex.hv_vp_set.valid_bank_mask;
1399c7012676SVitaly Kuznetsov 		all_cpus = flush_ex.hv_vp_set.format !=
1400c7012676SVitaly Kuznetsov 			HV_GENERIC_SET_SPARSE_4K;
1401c7012676SVitaly Kuznetsov 
14020b0a31baSVitaly Kuznetsov 		sparse_banks_len =
14030b0a31baSVitaly Kuznetsov 			bitmap_weight((unsigned long *)&valid_bank_mask, 64) *
1404c7012676SVitaly Kuznetsov 			sizeof(sparse_banks[0]);
1405c7012676SVitaly Kuznetsov 
1406c7012676SVitaly Kuznetsov 		if (!sparse_banks_len && !all_cpus)
1407c7012676SVitaly Kuznetsov 			goto ret_success;
1408c7012676SVitaly Kuznetsov 
1409c7012676SVitaly Kuznetsov 		if (!all_cpus &&
1410c7012676SVitaly Kuznetsov 		    kvm_read_guest(kvm,
1411c7012676SVitaly Kuznetsov 				   ingpa + offsetof(struct hv_tlb_flush_ex,
1412c7012676SVitaly Kuznetsov 						    hv_vp_set.bank_contents),
1413c7012676SVitaly Kuznetsov 				   sparse_banks,
1414c7012676SVitaly Kuznetsov 				   sparse_banks_len))
1415c7012676SVitaly Kuznetsov 			return HV_STATUS_INVALID_HYPERCALL_INPUT;
1416c7012676SVitaly Kuznetsov 	}
1417e2f11f42SVitaly Kuznetsov 
1418f21dd494SVitaly Kuznetsov 	cpumask_clear(&hv_vcpu->tlb_flush);
1419f21dd494SVitaly Kuznetsov 
1420f21dd494SVitaly Kuznetsov 	vcpu_mask = all_cpus ? NULL :
1421f21dd494SVitaly Kuznetsov 		sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask,
1422f21dd494SVitaly Kuznetsov 					vp_bitmap, vcpu_bitmap);
1423f21dd494SVitaly Kuznetsov 
14242cefc5feSVitaly Kuznetsov 	/*
14252cefc5feSVitaly Kuznetsov 	 * vcpu->arch.cr3 may not be up-to-date for running vCPUs so we can't
14262cefc5feSVitaly Kuznetsov 	 * analyze it here, flush TLB regardless of the specified address space.
14272cefc5feSVitaly Kuznetsov 	 */
1428a812297cSVitaly Kuznetsov 	kvm_make_vcpus_request_mask(kvm,
1429a812297cSVitaly Kuznetsov 				    KVM_REQ_TLB_FLUSH | KVM_REQUEST_NO_WAKEUP,
1430f21dd494SVitaly Kuznetsov 				    vcpu_mask, &hv_vcpu->tlb_flush);
1431e2f11f42SVitaly Kuznetsov 
1432c7012676SVitaly Kuznetsov ret_success:
1433e2f11f42SVitaly Kuznetsov 	/* We always do full TLB flush, set rep_done = rep_cnt. */
1434e2f11f42SVitaly Kuznetsov 	return (u64)HV_STATUS_SUCCESS |
1435e2f11f42SVitaly Kuznetsov 		((u64)rep_cnt << HV_HYPERCALL_REP_COMP_OFFSET);
1436e2f11f42SVitaly Kuznetsov }
1437e2f11f42SVitaly Kuznetsov 
1438f21dd494SVitaly Kuznetsov static void kvm_send_ipi_to_many(struct kvm *kvm, u32 vector,
1439f21dd494SVitaly Kuznetsov 				 unsigned long *vcpu_bitmap)
1440f21dd494SVitaly Kuznetsov {
1441f21dd494SVitaly Kuznetsov 	struct kvm_lapic_irq irq = {
1442f21dd494SVitaly Kuznetsov 		.delivery_mode = APIC_DM_FIXED,
1443f21dd494SVitaly Kuznetsov 		.vector = vector
1444f21dd494SVitaly Kuznetsov 	};
1445f21dd494SVitaly Kuznetsov 	struct kvm_vcpu *vcpu;
1446f21dd494SVitaly Kuznetsov 	int i;
1447f21dd494SVitaly Kuznetsov 
1448f21dd494SVitaly Kuznetsov 	kvm_for_each_vcpu(i, vcpu, kvm) {
1449f21dd494SVitaly Kuznetsov 		if (vcpu_bitmap && !test_bit(i, vcpu_bitmap))
1450f21dd494SVitaly Kuznetsov 			continue;
1451f21dd494SVitaly Kuznetsov 
1452f21dd494SVitaly Kuznetsov 		/* We fail only when APIC is disabled */
1453f21dd494SVitaly Kuznetsov 		kvm_apic_set_irq(vcpu, &irq, NULL);
1454f21dd494SVitaly Kuznetsov 	}
1455f21dd494SVitaly Kuznetsov }
1456f21dd494SVitaly Kuznetsov 
1457214ff83dSVitaly Kuznetsov static u64 kvm_hv_send_ipi(struct kvm_vcpu *current_vcpu, u64 ingpa, u64 outgpa,
1458214ff83dSVitaly Kuznetsov 			   bool ex, bool fast)
1459214ff83dSVitaly Kuznetsov {
1460214ff83dSVitaly Kuznetsov 	struct kvm *kvm = current_vcpu->kvm;
1461214ff83dSVitaly Kuznetsov 	struct hv_send_ipi_ex send_ipi_ex;
1462214ff83dSVitaly Kuznetsov 	struct hv_send_ipi send_ipi;
1463f21dd494SVitaly Kuznetsov 	u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS];
1464f21dd494SVitaly Kuznetsov 	DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS);
1465f21dd494SVitaly Kuznetsov 	unsigned long *vcpu_mask;
1466214ff83dSVitaly Kuznetsov 	unsigned long valid_bank_mask;
1467214ff83dSVitaly Kuznetsov 	u64 sparse_banks[64];
1468f21dd494SVitaly Kuznetsov 	int sparse_banks_len;
1469f21dd494SVitaly Kuznetsov 	u32 vector;
1470214ff83dSVitaly Kuznetsov 	bool all_cpus;
1471214ff83dSVitaly Kuznetsov 
1472214ff83dSVitaly Kuznetsov 	if (!ex) {
1473214ff83dSVitaly Kuznetsov 		if (!fast) {
1474214ff83dSVitaly Kuznetsov 			if (unlikely(kvm_read_guest(kvm, ingpa, &send_ipi,
1475214ff83dSVitaly Kuznetsov 						    sizeof(send_ipi))))
1476214ff83dSVitaly Kuznetsov 				return HV_STATUS_INVALID_HYPERCALL_INPUT;
1477214ff83dSVitaly Kuznetsov 			sparse_banks[0] = send_ipi.cpu_mask;
1478f21dd494SVitaly Kuznetsov 			vector = send_ipi.vector;
1479214ff83dSVitaly Kuznetsov 		} else {
1480214ff83dSVitaly Kuznetsov 			/* 'reserved' part of hv_send_ipi should be 0 */
1481214ff83dSVitaly Kuznetsov 			if (unlikely(ingpa >> 32 != 0))
1482214ff83dSVitaly Kuznetsov 				return HV_STATUS_INVALID_HYPERCALL_INPUT;
1483214ff83dSVitaly Kuznetsov 			sparse_banks[0] = outgpa;
1484f21dd494SVitaly Kuznetsov 			vector = (u32)ingpa;
1485214ff83dSVitaly Kuznetsov 		}
1486214ff83dSVitaly Kuznetsov 		all_cpus = false;
1487214ff83dSVitaly Kuznetsov 		valid_bank_mask = BIT_ULL(0);
1488214ff83dSVitaly Kuznetsov 
1489f21dd494SVitaly Kuznetsov 		trace_kvm_hv_send_ipi(vector, sparse_banks[0]);
1490214ff83dSVitaly Kuznetsov 	} else {
1491214ff83dSVitaly Kuznetsov 		if (unlikely(kvm_read_guest(kvm, ingpa, &send_ipi_ex,
1492214ff83dSVitaly Kuznetsov 					    sizeof(send_ipi_ex))))
1493214ff83dSVitaly Kuznetsov 			return HV_STATUS_INVALID_HYPERCALL_INPUT;
1494214ff83dSVitaly Kuznetsov 
1495214ff83dSVitaly Kuznetsov 		trace_kvm_hv_send_ipi_ex(send_ipi_ex.vector,
1496214ff83dSVitaly Kuznetsov 					 send_ipi_ex.vp_set.format,
1497214ff83dSVitaly Kuznetsov 					 send_ipi_ex.vp_set.valid_bank_mask);
1498214ff83dSVitaly Kuznetsov 
1499f21dd494SVitaly Kuznetsov 		vector = send_ipi_ex.vector;
1500214ff83dSVitaly Kuznetsov 		valid_bank_mask = send_ipi_ex.vp_set.valid_bank_mask;
1501214ff83dSVitaly Kuznetsov 		sparse_banks_len = bitmap_weight(&valid_bank_mask, 64) *
1502214ff83dSVitaly Kuznetsov 			sizeof(sparse_banks[0]);
1503214ff83dSVitaly Kuznetsov 
1504214ff83dSVitaly Kuznetsov 		all_cpus = send_ipi_ex.vp_set.format == HV_GENERIC_SET_ALL;
1505214ff83dSVitaly Kuznetsov 
1506214ff83dSVitaly Kuznetsov 		if (!sparse_banks_len)
1507214ff83dSVitaly Kuznetsov 			goto ret_success;
1508214ff83dSVitaly Kuznetsov 
1509214ff83dSVitaly Kuznetsov 		if (!all_cpus &&
1510214ff83dSVitaly Kuznetsov 		    kvm_read_guest(kvm,
1511214ff83dSVitaly Kuznetsov 				   ingpa + offsetof(struct hv_send_ipi_ex,
1512214ff83dSVitaly Kuznetsov 						    vp_set.bank_contents),
1513214ff83dSVitaly Kuznetsov 				   sparse_banks,
1514214ff83dSVitaly Kuznetsov 				   sparse_banks_len))
1515214ff83dSVitaly Kuznetsov 			return HV_STATUS_INVALID_HYPERCALL_INPUT;
1516214ff83dSVitaly Kuznetsov 	}
1517214ff83dSVitaly Kuznetsov 
1518f21dd494SVitaly Kuznetsov 	if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR))
1519214ff83dSVitaly Kuznetsov 		return HV_STATUS_INVALID_HYPERCALL_INPUT;
1520214ff83dSVitaly Kuznetsov 
1521f21dd494SVitaly Kuznetsov 	vcpu_mask = all_cpus ? NULL :
1522f21dd494SVitaly Kuznetsov 		sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask,
1523f21dd494SVitaly Kuznetsov 					vp_bitmap, vcpu_bitmap);
1524214ff83dSVitaly Kuznetsov 
1525f21dd494SVitaly Kuznetsov 	kvm_send_ipi_to_many(kvm, vector, vcpu_mask);
1526214ff83dSVitaly Kuznetsov 
1527214ff83dSVitaly Kuznetsov ret_success:
1528214ff83dSVitaly Kuznetsov 	return HV_STATUS_SUCCESS;
1529214ff83dSVitaly Kuznetsov }
1530214ff83dSVitaly Kuznetsov 
1531e83d5887SAndrey Smetanin bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1532e83d5887SAndrey Smetanin {
15333f5ad8beSPaolo Bonzini 	return READ_ONCE(kvm->arch.hyperv.hv_hypercall) & HV_X64_MSR_HYPERCALL_ENABLE;
1534e83d5887SAndrey Smetanin }
1535e83d5887SAndrey Smetanin 
153683326e43SAndrey Smetanin static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
153783326e43SAndrey Smetanin {
153883326e43SAndrey Smetanin 	bool longmode;
153983326e43SAndrey Smetanin 
154083326e43SAndrey Smetanin 	longmode = is_64_bit_mode(vcpu);
154183326e43SAndrey Smetanin 	if (longmode)
1542de3cd117SSean Christopherson 		kvm_rax_write(vcpu, result);
154383326e43SAndrey Smetanin 	else {
1544de3cd117SSean Christopherson 		kvm_rdx_write(vcpu, result >> 32);
1545de3cd117SSean Christopherson 		kvm_rax_write(vcpu, result & 0xffffffff);
154683326e43SAndrey Smetanin 	}
154783326e43SAndrey Smetanin }
154883326e43SAndrey Smetanin 
1549696ca779SRadim Krčmář static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result)
1550696ca779SRadim Krčmář {
1551696ca779SRadim Krčmář 	kvm_hv_hypercall_set_result(vcpu, result);
1552696ca779SRadim Krčmář 	++vcpu->stat.hypercalls;
1553696ca779SRadim Krčmář 	return kvm_skip_emulated_instruction(vcpu);
1554696ca779SRadim Krčmář }
1555696ca779SRadim Krčmář 
155683326e43SAndrey Smetanin static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
155783326e43SAndrey Smetanin {
1558696ca779SRadim Krčmář 	return kvm_hv_hypercall_complete(vcpu, vcpu->run->hyperv.u.hcall.result);
155983326e43SAndrey Smetanin }
156083326e43SAndrey Smetanin 
1561faeb7833SRoman Kagan static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, bool fast, u64 param)
1562faeb7833SRoman Kagan {
1563faeb7833SRoman Kagan 	struct eventfd_ctx *eventfd;
1564faeb7833SRoman Kagan 
1565faeb7833SRoman Kagan 	if (unlikely(!fast)) {
1566faeb7833SRoman Kagan 		int ret;
1567faeb7833SRoman Kagan 		gpa_t gpa = param;
1568faeb7833SRoman Kagan 
1569faeb7833SRoman Kagan 		if ((gpa & (__alignof__(param) - 1)) ||
1570faeb7833SRoman Kagan 		    offset_in_page(gpa) + sizeof(param) > PAGE_SIZE)
1571faeb7833SRoman Kagan 			return HV_STATUS_INVALID_ALIGNMENT;
1572faeb7833SRoman Kagan 
1573faeb7833SRoman Kagan 		ret = kvm_vcpu_read_guest(vcpu, gpa, &param, sizeof(param));
1574faeb7833SRoman Kagan 		if (ret < 0)
1575faeb7833SRoman Kagan 			return HV_STATUS_INVALID_ALIGNMENT;
1576faeb7833SRoman Kagan 	}
1577faeb7833SRoman Kagan 
1578faeb7833SRoman Kagan 	/*
1579faeb7833SRoman Kagan 	 * Per spec, bits 32-47 contain the extra "flag number".  However, we
1580faeb7833SRoman Kagan 	 * have no use for it, and in all known usecases it is zero, so just
1581faeb7833SRoman Kagan 	 * report lookup failure if it isn't.
1582faeb7833SRoman Kagan 	 */
1583faeb7833SRoman Kagan 	if (param & 0xffff00000000ULL)
1584faeb7833SRoman Kagan 		return HV_STATUS_INVALID_PORT_ID;
1585faeb7833SRoman Kagan 	/* remaining bits are reserved-zero */
1586faeb7833SRoman Kagan 	if (param & ~KVM_HYPERV_CONN_ID_MASK)
1587faeb7833SRoman Kagan 		return HV_STATUS_INVALID_HYPERCALL_INPUT;
1588faeb7833SRoman Kagan 
1589452a68d0SPaolo Bonzini 	/* the eventfd is protected by vcpu->kvm->srcu, but conn_to_evt isn't */
1590452a68d0SPaolo Bonzini 	rcu_read_lock();
1591faeb7833SRoman Kagan 	eventfd = idr_find(&vcpu->kvm->arch.hyperv.conn_to_evt, param);
1592452a68d0SPaolo Bonzini 	rcu_read_unlock();
1593faeb7833SRoman Kagan 	if (!eventfd)
1594faeb7833SRoman Kagan 		return HV_STATUS_INVALID_PORT_ID;
1595faeb7833SRoman Kagan 
1596faeb7833SRoman Kagan 	eventfd_signal(eventfd, 1);
1597faeb7833SRoman Kagan 	return HV_STATUS_SUCCESS;
1598faeb7833SRoman Kagan }
1599faeb7833SRoman Kagan 
1600e83d5887SAndrey Smetanin int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
1601e83d5887SAndrey Smetanin {
1602d32ef547SDan Carpenter 	u64 param, ingpa, outgpa, ret = HV_STATUS_SUCCESS;
1603d32ef547SDan Carpenter 	uint16_t code, rep_idx, rep_cnt;
1604f4e4805eSArnd Bergmann 	bool fast, rep;
1605e83d5887SAndrey Smetanin 
1606e83d5887SAndrey Smetanin 	/*
1607e83d5887SAndrey Smetanin 	 * hypercall generates UD from non zero cpl and real mode
1608e83d5887SAndrey Smetanin 	 * per HYPER-V spec
1609e83d5887SAndrey Smetanin 	 */
1610e83d5887SAndrey Smetanin 	if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
1611e83d5887SAndrey Smetanin 		kvm_queue_exception(vcpu, UD_VECTOR);
16120d9c055eSAndrey Smetanin 		return 1;
1613e83d5887SAndrey Smetanin 	}
1614e83d5887SAndrey Smetanin 
1615f4e4805eSArnd Bergmann #ifdef CONFIG_X86_64
1616f4e4805eSArnd Bergmann 	if (is_64_bit_mode(vcpu)) {
1617f4e4805eSArnd Bergmann 		param = kvm_rcx_read(vcpu);
1618f4e4805eSArnd Bergmann 		ingpa = kvm_rdx_read(vcpu);
1619f4e4805eSArnd Bergmann 		outgpa = kvm_r8_read(vcpu);
1620f4e4805eSArnd Bergmann 	} else
1621f4e4805eSArnd Bergmann #endif
1622f4e4805eSArnd Bergmann 	{
1623de3cd117SSean Christopherson 		param = ((u64)kvm_rdx_read(vcpu) << 32) |
1624de3cd117SSean Christopherson 			(kvm_rax_read(vcpu) & 0xffffffff);
1625de3cd117SSean Christopherson 		ingpa = ((u64)kvm_rbx_read(vcpu) << 32) |
1626de3cd117SSean Christopherson 			(kvm_rcx_read(vcpu) & 0xffffffff);
1627de3cd117SSean Christopherson 		outgpa = ((u64)kvm_rdi_read(vcpu) << 32) |
1628de3cd117SSean Christopherson 			(kvm_rsi_read(vcpu) & 0xffffffff);
1629e83d5887SAndrey Smetanin 	}
1630e83d5887SAndrey Smetanin 
1631e83d5887SAndrey Smetanin 	code = param & 0xffff;
1632142c95daSVitaly Kuznetsov 	fast = !!(param & HV_HYPERCALL_FAST_BIT);
1633142c95daSVitaly Kuznetsov 	rep_cnt = (param >> HV_HYPERCALL_REP_COMP_OFFSET) & 0xfff;
1634142c95daSVitaly Kuznetsov 	rep_idx = (param >> HV_HYPERCALL_REP_START_OFFSET) & 0xfff;
163556b9ae78SVitaly Kuznetsov 	rep = !!(rep_cnt || rep_idx);
1636e83d5887SAndrey Smetanin 
1637e83d5887SAndrey Smetanin 	trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
1638e83d5887SAndrey Smetanin 
1639e83d5887SAndrey Smetanin 	switch (code) {
16408ed6d767SAndrey Smetanin 	case HVCALL_NOTIFY_LONG_SPIN_WAIT:
164156b9ae78SVitaly Kuznetsov 		if (unlikely(rep)) {
164256b9ae78SVitaly Kuznetsov 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
164356b9ae78SVitaly Kuznetsov 			break;
164456b9ae78SVitaly Kuznetsov 		}
1645de63ad4cSLongpeng(Mike) 		kvm_vcpu_on_spin(vcpu, true);
1646e83d5887SAndrey Smetanin 		break;
164783326e43SAndrey Smetanin 	case HVCALL_SIGNAL_EVENT:
164856b9ae78SVitaly Kuznetsov 		if (unlikely(rep)) {
164956b9ae78SVitaly Kuznetsov 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
165056b9ae78SVitaly Kuznetsov 			break;
165156b9ae78SVitaly Kuznetsov 		}
1652d32ef547SDan Carpenter 		ret = kvm_hvcall_signal_event(vcpu, fast, ingpa);
1653d32ef547SDan Carpenter 		if (ret != HV_STATUS_INVALID_PORT_ID)
1654faeb7833SRoman Kagan 			break;
1655b2869f28SGustavo A. R. Silva 		/* fall through - maybe userspace knows this conn_id. */
1656faeb7833SRoman Kagan 	case HVCALL_POST_MESSAGE:
1657a2b5c3c0SPaolo Bonzini 		/* don't bother userspace if it has no way to handle it */
165856b9ae78SVitaly Kuznetsov 		if (unlikely(rep || !vcpu_to_synic(vcpu)->active)) {
165956b9ae78SVitaly Kuznetsov 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1660a2b5c3c0SPaolo Bonzini 			break;
1661a2b5c3c0SPaolo Bonzini 		}
166283326e43SAndrey Smetanin 		vcpu->run->exit_reason = KVM_EXIT_HYPERV;
166383326e43SAndrey Smetanin 		vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
166483326e43SAndrey Smetanin 		vcpu->run->hyperv.u.hcall.input = param;
166583326e43SAndrey Smetanin 		vcpu->run->hyperv.u.hcall.params[0] = ingpa;
166683326e43SAndrey Smetanin 		vcpu->run->hyperv.u.hcall.params[1] = outgpa;
166783326e43SAndrey Smetanin 		vcpu->arch.complete_userspace_io =
166883326e43SAndrey Smetanin 				kvm_hv_hypercall_complete_userspace;
166983326e43SAndrey Smetanin 		return 0;
1670e2f11f42SVitaly Kuznetsov 	case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST:
1671e2f11f42SVitaly Kuznetsov 		if (unlikely(fast || !rep_cnt || rep_idx)) {
1672e2f11f42SVitaly Kuznetsov 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1673e2f11f42SVitaly Kuznetsov 			break;
1674e2f11f42SVitaly Kuznetsov 		}
1675c7012676SVitaly Kuznetsov 		ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, false);
1676e2f11f42SVitaly Kuznetsov 		break;
1677e2f11f42SVitaly Kuznetsov 	case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE:
1678e2f11f42SVitaly Kuznetsov 		if (unlikely(fast || rep)) {
1679e2f11f42SVitaly Kuznetsov 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1680e2f11f42SVitaly Kuznetsov 			break;
1681e2f11f42SVitaly Kuznetsov 		}
1682c7012676SVitaly Kuznetsov 		ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, false);
1683c7012676SVitaly Kuznetsov 		break;
1684c7012676SVitaly Kuznetsov 	case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX:
1685c7012676SVitaly Kuznetsov 		if (unlikely(fast || !rep_cnt || rep_idx)) {
1686c7012676SVitaly Kuznetsov 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1687c7012676SVitaly Kuznetsov 			break;
1688c7012676SVitaly Kuznetsov 		}
1689c7012676SVitaly Kuznetsov 		ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, true);
1690c7012676SVitaly Kuznetsov 		break;
1691c7012676SVitaly Kuznetsov 	case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX:
1692c7012676SVitaly Kuznetsov 		if (unlikely(fast || rep)) {
1693c7012676SVitaly Kuznetsov 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1694c7012676SVitaly Kuznetsov 			break;
1695c7012676SVitaly Kuznetsov 		}
1696c7012676SVitaly Kuznetsov 		ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, true);
1697e2f11f42SVitaly Kuznetsov 		break;
1698214ff83dSVitaly Kuznetsov 	case HVCALL_SEND_IPI:
1699214ff83dSVitaly Kuznetsov 		if (unlikely(rep)) {
1700214ff83dSVitaly Kuznetsov 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1701214ff83dSVitaly Kuznetsov 			break;
1702214ff83dSVitaly Kuznetsov 		}
1703214ff83dSVitaly Kuznetsov 		ret = kvm_hv_send_ipi(vcpu, ingpa, outgpa, false, fast);
1704214ff83dSVitaly Kuznetsov 		break;
1705214ff83dSVitaly Kuznetsov 	case HVCALL_SEND_IPI_EX:
1706214ff83dSVitaly Kuznetsov 		if (unlikely(fast || rep)) {
1707214ff83dSVitaly Kuznetsov 			ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1708214ff83dSVitaly Kuznetsov 			break;
1709214ff83dSVitaly Kuznetsov 		}
1710214ff83dSVitaly Kuznetsov 		ret = kvm_hv_send_ipi(vcpu, ingpa, outgpa, true, false);
1711214ff83dSVitaly Kuznetsov 		break;
1712e83d5887SAndrey Smetanin 	default:
1713d32ef547SDan Carpenter 		ret = HV_STATUS_INVALID_HYPERCALL_CODE;
1714e83d5887SAndrey Smetanin 		break;
1715e83d5887SAndrey Smetanin 	}
1716e83d5887SAndrey Smetanin 
1717696ca779SRadim Krčmář 	return kvm_hv_hypercall_complete(vcpu, ret);
1718e83d5887SAndrey Smetanin }
1719cbc0236aSRoman Kagan 
1720cbc0236aSRoman Kagan void kvm_hv_init_vm(struct kvm *kvm)
1721cbc0236aSRoman Kagan {
1722cbc0236aSRoman Kagan 	mutex_init(&kvm->arch.hyperv.hv_lock);
1723faeb7833SRoman Kagan 	idr_init(&kvm->arch.hyperv.conn_to_evt);
1724cbc0236aSRoman Kagan }
1725cbc0236aSRoman Kagan 
1726cbc0236aSRoman Kagan void kvm_hv_destroy_vm(struct kvm *kvm)
1727cbc0236aSRoman Kagan {
1728faeb7833SRoman Kagan 	struct eventfd_ctx *eventfd;
1729faeb7833SRoman Kagan 	int i;
1730faeb7833SRoman Kagan 
1731faeb7833SRoman Kagan 	idr_for_each_entry(&kvm->arch.hyperv.conn_to_evt, eventfd, i)
1732faeb7833SRoman Kagan 		eventfd_ctx_put(eventfd);
1733faeb7833SRoman Kagan 	idr_destroy(&kvm->arch.hyperv.conn_to_evt);
1734faeb7833SRoman Kagan }
1735faeb7833SRoman Kagan 
1736faeb7833SRoman Kagan static int kvm_hv_eventfd_assign(struct kvm *kvm, u32 conn_id, int fd)
1737faeb7833SRoman Kagan {
1738faeb7833SRoman Kagan 	struct kvm_hv *hv = &kvm->arch.hyperv;
1739faeb7833SRoman Kagan 	struct eventfd_ctx *eventfd;
1740faeb7833SRoman Kagan 	int ret;
1741faeb7833SRoman Kagan 
1742faeb7833SRoman Kagan 	eventfd = eventfd_ctx_fdget(fd);
1743faeb7833SRoman Kagan 	if (IS_ERR(eventfd))
1744faeb7833SRoman Kagan 		return PTR_ERR(eventfd);
1745faeb7833SRoman Kagan 
1746faeb7833SRoman Kagan 	mutex_lock(&hv->hv_lock);
1747faeb7833SRoman Kagan 	ret = idr_alloc(&hv->conn_to_evt, eventfd, conn_id, conn_id + 1,
1748254272ceSBen Gardon 			GFP_KERNEL_ACCOUNT);
1749faeb7833SRoman Kagan 	mutex_unlock(&hv->hv_lock);
1750faeb7833SRoman Kagan 
1751faeb7833SRoman Kagan 	if (ret >= 0)
1752faeb7833SRoman Kagan 		return 0;
1753faeb7833SRoman Kagan 
1754faeb7833SRoman Kagan 	if (ret == -ENOSPC)
1755faeb7833SRoman Kagan 		ret = -EEXIST;
1756faeb7833SRoman Kagan 	eventfd_ctx_put(eventfd);
1757faeb7833SRoman Kagan 	return ret;
1758faeb7833SRoman Kagan }
1759faeb7833SRoman Kagan 
1760faeb7833SRoman Kagan static int kvm_hv_eventfd_deassign(struct kvm *kvm, u32 conn_id)
1761faeb7833SRoman Kagan {
1762faeb7833SRoman Kagan 	struct kvm_hv *hv = &kvm->arch.hyperv;
1763faeb7833SRoman Kagan 	struct eventfd_ctx *eventfd;
1764faeb7833SRoman Kagan 
1765faeb7833SRoman Kagan 	mutex_lock(&hv->hv_lock);
1766faeb7833SRoman Kagan 	eventfd = idr_remove(&hv->conn_to_evt, conn_id);
1767faeb7833SRoman Kagan 	mutex_unlock(&hv->hv_lock);
1768faeb7833SRoman Kagan 
1769faeb7833SRoman Kagan 	if (!eventfd)
1770faeb7833SRoman Kagan 		return -ENOENT;
1771faeb7833SRoman Kagan 
1772faeb7833SRoman Kagan 	synchronize_srcu(&kvm->srcu);
1773faeb7833SRoman Kagan 	eventfd_ctx_put(eventfd);
1774faeb7833SRoman Kagan 	return 0;
1775faeb7833SRoman Kagan }
1776faeb7833SRoman Kagan 
1777faeb7833SRoman Kagan int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args)
1778faeb7833SRoman Kagan {
1779faeb7833SRoman Kagan 	if ((args->flags & ~KVM_HYPERV_EVENTFD_DEASSIGN) ||
1780faeb7833SRoman Kagan 	    (args->conn_id & ~KVM_HYPERV_CONN_ID_MASK))
1781faeb7833SRoman Kagan 		return -EINVAL;
1782faeb7833SRoman Kagan 
1783faeb7833SRoman Kagan 	if (args->flags == KVM_HYPERV_EVENTFD_DEASSIGN)
1784faeb7833SRoman Kagan 		return kvm_hv_eventfd_deassign(kvm, args->conn_id);
1785faeb7833SRoman Kagan 	return kvm_hv_eventfd_assign(kvm, args->conn_id, args->fd);
1786cbc0236aSRoman Kagan }
17872bc39970SVitaly Kuznetsov 
17882bc39970SVitaly Kuznetsov int kvm_vcpu_ioctl_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,
17892bc39970SVitaly Kuznetsov 				struct kvm_cpuid_entry2 __user *entries)
17902bc39970SVitaly Kuznetsov {
1791ea152987SVitaly Kuznetsov 	uint16_t evmcs_ver = 0;
17922bc39970SVitaly Kuznetsov 	struct kvm_cpuid_entry2 cpuid_entries[] = {
17932bc39970SVitaly Kuznetsov 		{ .function = HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS },
17942bc39970SVitaly Kuznetsov 		{ .function = HYPERV_CPUID_INTERFACE },
17952bc39970SVitaly Kuznetsov 		{ .function = HYPERV_CPUID_VERSION },
17962bc39970SVitaly Kuznetsov 		{ .function = HYPERV_CPUID_FEATURES },
17972bc39970SVitaly Kuznetsov 		{ .function = HYPERV_CPUID_ENLIGHTMENT_INFO },
17982bc39970SVitaly Kuznetsov 		{ .function = HYPERV_CPUID_IMPLEMENT_LIMITS },
17992bc39970SVitaly Kuznetsov 		{ .function = HYPERV_CPUID_NESTED_FEATURES },
18002bc39970SVitaly Kuznetsov 	};
18012bc39970SVitaly Kuznetsov 	int i, nent = ARRAY_SIZE(cpuid_entries);
18022bc39970SVitaly Kuznetsov 
1803ea152987SVitaly Kuznetsov 	if (kvm_x86_ops->nested_get_evmcs_version)
1804ea152987SVitaly Kuznetsov 		evmcs_ver = kvm_x86_ops->nested_get_evmcs_version(vcpu);
1805ea152987SVitaly Kuznetsov 
18062bc39970SVitaly Kuznetsov 	/* Skip NESTED_FEATURES if eVMCS is not supported */
18072bc39970SVitaly Kuznetsov 	if (!evmcs_ver)
18082bc39970SVitaly Kuznetsov 		--nent;
18092bc39970SVitaly Kuznetsov 
18102bc39970SVitaly Kuznetsov 	if (cpuid->nent < nent)
18112bc39970SVitaly Kuznetsov 		return -E2BIG;
18122bc39970SVitaly Kuznetsov 
18132bc39970SVitaly Kuznetsov 	if (cpuid->nent > nent)
18142bc39970SVitaly Kuznetsov 		cpuid->nent = nent;
18152bc39970SVitaly Kuznetsov 
18162bc39970SVitaly Kuznetsov 	for (i = 0; i < nent; i++) {
18172bc39970SVitaly Kuznetsov 		struct kvm_cpuid_entry2 *ent = &cpuid_entries[i];
18182bc39970SVitaly Kuznetsov 		u32 signature[3];
18192bc39970SVitaly Kuznetsov 
18202bc39970SVitaly Kuznetsov 		switch (ent->function) {
18212bc39970SVitaly Kuznetsov 		case HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS:
18222bc39970SVitaly Kuznetsov 			memcpy(signature, "Linux KVM Hv", 12);
18232bc39970SVitaly Kuznetsov 
18242bc39970SVitaly Kuznetsov 			ent->eax = HYPERV_CPUID_NESTED_FEATURES;
18252bc39970SVitaly Kuznetsov 			ent->ebx = signature[0];
18262bc39970SVitaly Kuznetsov 			ent->ecx = signature[1];
18272bc39970SVitaly Kuznetsov 			ent->edx = signature[2];
18282bc39970SVitaly Kuznetsov 			break;
18292bc39970SVitaly Kuznetsov 
18302bc39970SVitaly Kuznetsov 		case HYPERV_CPUID_INTERFACE:
18312bc39970SVitaly Kuznetsov 			memcpy(signature, "Hv#1\0\0\0\0\0\0\0\0", 12);
18322bc39970SVitaly Kuznetsov 			ent->eax = signature[0];
18332bc39970SVitaly Kuznetsov 			break;
18342bc39970SVitaly Kuznetsov 
18352bc39970SVitaly Kuznetsov 		case HYPERV_CPUID_VERSION:
18362bc39970SVitaly Kuznetsov 			/*
18372bc39970SVitaly Kuznetsov 			 * We implement some Hyper-V 2016 functions so let's use
18382bc39970SVitaly Kuznetsov 			 * this version.
18392bc39970SVitaly Kuznetsov 			 */
18402bc39970SVitaly Kuznetsov 			ent->eax = 0x00003839;
18412bc39970SVitaly Kuznetsov 			ent->ebx = 0x000A0000;
18422bc39970SVitaly Kuznetsov 			break;
18432bc39970SVitaly Kuznetsov 
18442bc39970SVitaly Kuznetsov 		case HYPERV_CPUID_FEATURES:
18452bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_MSR_VP_RUNTIME_AVAILABLE;
18462bc39970SVitaly Kuznetsov 			ent->eax |= HV_MSR_TIME_REF_COUNT_AVAILABLE;
18472bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_MSR_SYNIC_AVAILABLE;
18482bc39970SVitaly Kuznetsov 			ent->eax |= HV_MSR_SYNTIMER_AVAILABLE;
18492bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_MSR_APIC_ACCESS_AVAILABLE;
18502bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_MSR_HYPERCALL_AVAILABLE;
18512bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_MSR_VP_INDEX_AVAILABLE;
18522bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_MSR_RESET_AVAILABLE;
18532bc39970SVitaly Kuznetsov 			ent->eax |= HV_MSR_REFERENCE_TSC_AVAILABLE;
18542bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_ACCESS_FREQUENCY_MSRS;
18552bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_ACCESS_REENLIGHTENMENT;
18562bc39970SVitaly Kuznetsov 
18572bc39970SVitaly Kuznetsov 			ent->ebx |= HV_X64_POST_MESSAGES;
18582bc39970SVitaly Kuznetsov 			ent->ebx |= HV_X64_SIGNAL_EVENTS;
18592bc39970SVitaly Kuznetsov 
18602bc39970SVitaly Kuznetsov 			ent->edx |= HV_FEATURE_FREQUENCY_MSRS_AVAILABLE;
18612bc39970SVitaly Kuznetsov 			ent->edx |= HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE;
1862a073d7e3SWanpeng Li 
1863a073d7e3SWanpeng Li 			/*
1864a073d7e3SWanpeng Li 			 * Direct Synthetic timers only make sense with in-kernel
1865a073d7e3SWanpeng Li 			 * LAPIC
1866a073d7e3SWanpeng Li 			 */
1867a073d7e3SWanpeng Li 			if (lapic_in_kernel(vcpu))
18682bc39970SVitaly Kuznetsov 				ent->edx |= HV_STIMER_DIRECT_MODE_AVAILABLE;
18692bc39970SVitaly Kuznetsov 
18702bc39970SVitaly Kuznetsov 			break;
18712bc39970SVitaly Kuznetsov 
18722bc39970SVitaly Kuznetsov 		case HYPERV_CPUID_ENLIGHTMENT_INFO:
18732bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED;
18742bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_APIC_ACCESS_RECOMMENDED;
18752bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED;
18762bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_CLUSTER_IPI_RECOMMENDED;
18772bc39970SVitaly Kuznetsov 			ent->eax |= HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED;
1878f1adceafSVitaly Kuznetsov 			if (evmcs_ver)
18792bc39970SVitaly Kuznetsov 				ent->eax |= HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
1880b2d8b167SVitaly Kuznetsov 			if (!cpu_smt_possible())
1881b2d8b167SVitaly Kuznetsov 				ent->eax |= HV_X64_NO_NONARCH_CORESHARING;
18822bc39970SVitaly Kuznetsov 			/*
18832bc39970SVitaly Kuznetsov 			 * Default number of spinlock retry attempts, matches
18842bc39970SVitaly Kuznetsov 			 * HyperV 2016.
18852bc39970SVitaly Kuznetsov 			 */
18862bc39970SVitaly Kuznetsov 			ent->ebx = 0x00000FFF;
18872bc39970SVitaly Kuznetsov 
18882bc39970SVitaly Kuznetsov 			break;
18892bc39970SVitaly Kuznetsov 
18902bc39970SVitaly Kuznetsov 		case HYPERV_CPUID_IMPLEMENT_LIMITS:
18912bc39970SVitaly Kuznetsov 			/* Maximum number of virtual processors */
18922bc39970SVitaly Kuznetsov 			ent->eax = KVM_MAX_VCPUS;
18932bc39970SVitaly Kuznetsov 			/*
18942bc39970SVitaly Kuznetsov 			 * Maximum number of logical processors, matches
18952bc39970SVitaly Kuznetsov 			 * HyperV 2016.
18962bc39970SVitaly Kuznetsov 			 */
18972bc39970SVitaly Kuznetsov 			ent->ebx = 64;
18982bc39970SVitaly Kuznetsov 
18992bc39970SVitaly Kuznetsov 			break;
19002bc39970SVitaly Kuznetsov 
19012bc39970SVitaly Kuznetsov 		case HYPERV_CPUID_NESTED_FEATURES:
19022bc39970SVitaly Kuznetsov 			ent->eax = evmcs_ver;
19032bc39970SVitaly Kuznetsov 
19042bc39970SVitaly Kuznetsov 			break;
19052bc39970SVitaly Kuznetsov 
19062bc39970SVitaly Kuznetsov 		default:
19072bc39970SVitaly Kuznetsov 			break;
19082bc39970SVitaly Kuznetsov 		}
19092bc39970SVitaly Kuznetsov 	}
19102bc39970SVitaly Kuznetsov 
19112bc39970SVitaly Kuznetsov 	if (copy_to_user(entries, cpuid_entries,
19122bc39970SVitaly Kuznetsov 			 nent * sizeof(struct kvm_cpuid_entry2)))
19132bc39970SVitaly Kuznetsov 		return -EFAULT;
19142bc39970SVitaly Kuznetsov 
19152bc39970SVitaly Kuznetsov 	return 0;
19162bc39970SVitaly Kuznetsov }
1917