xref: /openbmc/linux/arch/x86/kvm/hyperv.c (revision db3975717ac5e2c2761bae7b90c4f2e0abb5ef22)
1e83d5887SAndrey Smetanin /*
2e83d5887SAndrey Smetanin  * KVM Microsoft Hyper-V emulation
3e83d5887SAndrey Smetanin  *
4e83d5887SAndrey Smetanin  * derived from arch/x86/kvm/x86.c
5e83d5887SAndrey Smetanin  *
6e83d5887SAndrey Smetanin  * Copyright (C) 2006 Qumranet, Inc.
7e83d5887SAndrey Smetanin  * Copyright (C) 2008 Qumranet, Inc.
8e83d5887SAndrey Smetanin  * Copyright IBM Corporation, 2008
9e83d5887SAndrey Smetanin  * Copyright 2010 Red Hat, Inc. and/or its affiliates.
10e83d5887SAndrey Smetanin  * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com>
11e83d5887SAndrey Smetanin  *
12e83d5887SAndrey Smetanin  * Authors:
13e83d5887SAndrey Smetanin  *   Avi Kivity   <avi@qumranet.com>
14e83d5887SAndrey Smetanin  *   Yaniv Kamay  <yaniv@qumranet.com>
15e83d5887SAndrey Smetanin  *   Amit Shah    <amit.shah@qumranet.com>
16e83d5887SAndrey Smetanin  *   Ben-Ami Yassour <benami@il.ibm.com>
17e83d5887SAndrey Smetanin  *   Andrey Smetanin <asmetanin@virtuozzo.com>
18e83d5887SAndrey Smetanin  *
19e83d5887SAndrey Smetanin  * This work is licensed under the terms of the GNU GPL, version 2.  See
20e83d5887SAndrey Smetanin  * the COPYING file in the top-level directory.
21e83d5887SAndrey Smetanin  *
22e83d5887SAndrey Smetanin  */
23e83d5887SAndrey Smetanin 
24e83d5887SAndrey Smetanin #include "x86.h"
25e83d5887SAndrey Smetanin #include "lapic.h"
265c919412SAndrey Smetanin #include "ioapic.h"
27e83d5887SAndrey Smetanin #include "hyperv.h"
28e83d5887SAndrey Smetanin 
29e83d5887SAndrey Smetanin #include <linux/kvm_host.h>
305c919412SAndrey Smetanin #include <asm/apicdef.h>
31e83d5887SAndrey Smetanin #include <trace/events/kvm.h>
32e83d5887SAndrey Smetanin 
33e83d5887SAndrey Smetanin #include "trace.h"
34e83d5887SAndrey Smetanin 
355c919412SAndrey Smetanin static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
365c919412SAndrey Smetanin {
375c919412SAndrey Smetanin 	return atomic64_read(&synic->sint[sint]);
385c919412SAndrey Smetanin }
395c919412SAndrey Smetanin 
405c919412SAndrey Smetanin static inline int synic_get_sint_vector(u64 sint_value)
415c919412SAndrey Smetanin {
425c919412SAndrey Smetanin 	if (sint_value & HV_SYNIC_SINT_MASKED)
435c919412SAndrey Smetanin 		return -1;
445c919412SAndrey Smetanin 	return sint_value & HV_SYNIC_SINT_VECTOR_MASK;
455c919412SAndrey Smetanin }
465c919412SAndrey Smetanin 
475c919412SAndrey Smetanin static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic,
485c919412SAndrey Smetanin 				      int vector)
495c919412SAndrey Smetanin {
505c919412SAndrey Smetanin 	int i;
515c919412SAndrey Smetanin 
525c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
535c919412SAndrey Smetanin 		if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
545c919412SAndrey Smetanin 			return true;
555c919412SAndrey Smetanin 	}
565c919412SAndrey Smetanin 	return false;
575c919412SAndrey Smetanin }
585c919412SAndrey Smetanin 
595c919412SAndrey Smetanin static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic,
605c919412SAndrey Smetanin 				     int vector)
615c919412SAndrey Smetanin {
625c919412SAndrey Smetanin 	int i;
635c919412SAndrey Smetanin 	u64 sint_value;
645c919412SAndrey Smetanin 
655c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
665c919412SAndrey Smetanin 		sint_value = synic_read_sint(synic, i);
675c919412SAndrey Smetanin 		if (synic_get_sint_vector(sint_value) == vector &&
685c919412SAndrey Smetanin 		    sint_value & HV_SYNIC_SINT_AUTO_EOI)
695c919412SAndrey Smetanin 			return true;
705c919412SAndrey Smetanin 	}
715c919412SAndrey Smetanin 	return false;
725c919412SAndrey Smetanin }
735c919412SAndrey Smetanin 
745c919412SAndrey Smetanin static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint, u64 data)
755c919412SAndrey Smetanin {
765c919412SAndrey Smetanin 	int vector;
775c919412SAndrey Smetanin 
785c919412SAndrey Smetanin 	vector = data & HV_SYNIC_SINT_VECTOR_MASK;
795c919412SAndrey Smetanin 	if (vector < 16)
805c919412SAndrey Smetanin 		return 1;
815c919412SAndrey Smetanin 	/*
825c919412SAndrey Smetanin 	 * Guest may configure multiple SINTs to use the same vector, so
835c919412SAndrey Smetanin 	 * we maintain a bitmap of vectors handled by synic, and a
845c919412SAndrey Smetanin 	 * bitmap of vectors with auto-eoi behavior.  The bitmaps are
855c919412SAndrey Smetanin 	 * updated here, and atomically queried on fast paths.
865c919412SAndrey Smetanin 	 */
875c919412SAndrey Smetanin 
885c919412SAndrey Smetanin 	atomic64_set(&synic->sint[sint], data);
895c919412SAndrey Smetanin 
905c919412SAndrey Smetanin 	if (synic_has_vector_connected(synic, vector))
915c919412SAndrey Smetanin 		__set_bit(vector, synic->vec_bitmap);
925c919412SAndrey Smetanin 	else
935c919412SAndrey Smetanin 		__clear_bit(vector, synic->vec_bitmap);
945c919412SAndrey Smetanin 
955c919412SAndrey Smetanin 	if (synic_has_vector_auto_eoi(synic, vector))
965c919412SAndrey Smetanin 		__set_bit(vector, synic->auto_eoi_bitmap);
975c919412SAndrey Smetanin 	else
985c919412SAndrey Smetanin 		__clear_bit(vector, synic->auto_eoi_bitmap);
995c919412SAndrey Smetanin 
1005c919412SAndrey Smetanin 	/* Load SynIC vectors into EOI exit bitmap */
1015c919412SAndrey Smetanin 	kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic));
1025c919412SAndrey Smetanin 	return 0;
1035c919412SAndrey Smetanin }
1045c919412SAndrey Smetanin 
1055c919412SAndrey Smetanin static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vcpu_id)
1065c919412SAndrey Smetanin {
1075c919412SAndrey Smetanin 	struct kvm_vcpu *vcpu;
1085c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic;
1095c919412SAndrey Smetanin 
1105c919412SAndrey Smetanin 	if (vcpu_id >= atomic_read(&kvm->online_vcpus))
1115c919412SAndrey Smetanin 		return NULL;
1125c919412SAndrey Smetanin 	vcpu = kvm_get_vcpu(kvm, vcpu_id);
1135c919412SAndrey Smetanin 	if (!vcpu)
1145c919412SAndrey Smetanin 		return NULL;
1155c919412SAndrey Smetanin 	synic = vcpu_to_synic(vcpu);
1165c919412SAndrey Smetanin 	return (synic->active) ? synic : NULL;
1175c919412SAndrey Smetanin }
1185c919412SAndrey Smetanin 
1195c919412SAndrey Smetanin static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
1205c919412SAndrey Smetanin {
1215c919412SAndrey Smetanin 	struct kvm *kvm = vcpu->kvm;
1225c919412SAndrey Smetanin 	int gsi, idx;
1235c919412SAndrey Smetanin 
1245c919412SAndrey Smetanin 	vcpu_debug(vcpu, "Hyper-V SynIC acked sint %d\n", sint);
1255c919412SAndrey Smetanin 
1265c919412SAndrey Smetanin 	idx = srcu_read_lock(&kvm->irq_srcu);
1275c919412SAndrey Smetanin 	gsi = atomic_read(&vcpu_to_synic(vcpu)->sint_to_gsi[sint]);
1285c919412SAndrey Smetanin 	if (gsi != -1)
1295c919412SAndrey Smetanin 		kvm_notify_acked_gsi(kvm, gsi);
1305c919412SAndrey Smetanin 	srcu_read_unlock(&kvm->irq_srcu, idx);
1315c919412SAndrey Smetanin }
1325c919412SAndrey Smetanin 
133*db397571SAndrey Smetanin static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
134*db397571SAndrey Smetanin {
135*db397571SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
136*db397571SAndrey Smetanin 	struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
137*db397571SAndrey Smetanin 
138*db397571SAndrey Smetanin 	hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
139*db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.msr = msr;
140*db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.control = synic->control;
141*db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
142*db397571SAndrey Smetanin 	hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
143*db397571SAndrey Smetanin 
144*db397571SAndrey Smetanin 	kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
145*db397571SAndrey Smetanin }
146*db397571SAndrey Smetanin 
1475c919412SAndrey Smetanin static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
1485c919412SAndrey Smetanin 			 u32 msr, u64 data, bool host)
1495c919412SAndrey Smetanin {
1505c919412SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
1515c919412SAndrey Smetanin 	int ret;
1525c919412SAndrey Smetanin 
1535c919412SAndrey Smetanin 	if (!synic->active)
1545c919412SAndrey Smetanin 		return 1;
1555c919412SAndrey Smetanin 
1565c919412SAndrey Smetanin 	vcpu_debug(vcpu, "Hyper-V SynIC set msr 0x%x 0x%llx host %d\n",
1575c919412SAndrey Smetanin 		   msr, data, host);
1585c919412SAndrey Smetanin 	ret = 0;
1595c919412SAndrey Smetanin 	switch (msr) {
1605c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
1615c919412SAndrey Smetanin 		synic->control = data;
162*db397571SAndrey Smetanin 		if (!host)
163*db397571SAndrey Smetanin 			synic_exit(synic, msr);
1645c919412SAndrey Smetanin 		break;
1655c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
1665c919412SAndrey Smetanin 		if (!host) {
1675c919412SAndrey Smetanin 			ret = 1;
1685c919412SAndrey Smetanin 			break;
1695c919412SAndrey Smetanin 		}
1705c919412SAndrey Smetanin 		synic->version = data;
1715c919412SAndrey Smetanin 		break;
1725c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
1735c919412SAndrey Smetanin 		if (data & HV_SYNIC_SIEFP_ENABLE)
1745c919412SAndrey Smetanin 			if (kvm_clear_guest(vcpu->kvm,
1755c919412SAndrey Smetanin 					    data & PAGE_MASK, PAGE_SIZE)) {
1765c919412SAndrey Smetanin 				ret = 1;
1775c919412SAndrey Smetanin 				break;
1785c919412SAndrey Smetanin 			}
1795c919412SAndrey Smetanin 		synic->evt_page = data;
180*db397571SAndrey Smetanin 		if (!host)
181*db397571SAndrey Smetanin 			synic_exit(synic, msr);
1825c919412SAndrey Smetanin 		break;
1835c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
1845c919412SAndrey Smetanin 		if (data & HV_SYNIC_SIMP_ENABLE)
1855c919412SAndrey Smetanin 			if (kvm_clear_guest(vcpu->kvm,
1865c919412SAndrey Smetanin 					    data & PAGE_MASK, PAGE_SIZE)) {
1875c919412SAndrey Smetanin 				ret = 1;
1885c919412SAndrey Smetanin 				break;
1895c919412SAndrey Smetanin 			}
1905c919412SAndrey Smetanin 		synic->msg_page = data;
191*db397571SAndrey Smetanin 		if (!host)
192*db397571SAndrey Smetanin 			synic_exit(synic, msr);
1935c919412SAndrey Smetanin 		break;
1945c919412SAndrey Smetanin 	case HV_X64_MSR_EOM: {
1955c919412SAndrey Smetanin 		int i;
1965c919412SAndrey Smetanin 
1975c919412SAndrey Smetanin 		for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
1985c919412SAndrey Smetanin 			kvm_hv_notify_acked_sint(vcpu, i);
1995c919412SAndrey Smetanin 		break;
2005c919412SAndrey Smetanin 	}
2015c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
2025c919412SAndrey Smetanin 		ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data);
2035c919412SAndrey Smetanin 		break;
2045c919412SAndrey Smetanin 	default:
2055c919412SAndrey Smetanin 		ret = 1;
2065c919412SAndrey Smetanin 		break;
2075c919412SAndrey Smetanin 	}
2085c919412SAndrey Smetanin 	return ret;
2095c919412SAndrey Smetanin }
2105c919412SAndrey Smetanin 
2115c919412SAndrey Smetanin static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata)
2125c919412SAndrey Smetanin {
2135c919412SAndrey Smetanin 	int ret;
2145c919412SAndrey Smetanin 
2155c919412SAndrey Smetanin 	if (!synic->active)
2165c919412SAndrey Smetanin 		return 1;
2175c919412SAndrey Smetanin 
2185c919412SAndrey Smetanin 	ret = 0;
2195c919412SAndrey Smetanin 	switch (msr) {
2205c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
2215c919412SAndrey Smetanin 		*pdata = synic->control;
2225c919412SAndrey Smetanin 		break;
2235c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
2245c919412SAndrey Smetanin 		*pdata = synic->version;
2255c919412SAndrey Smetanin 		break;
2265c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
2275c919412SAndrey Smetanin 		*pdata = synic->evt_page;
2285c919412SAndrey Smetanin 		break;
2295c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
2305c919412SAndrey Smetanin 		*pdata = synic->msg_page;
2315c919412SAndrey Smetanin 		break;
2325c919412SAndrey Smetanin 	case HV_X64_MSR_EOM:
2335c919412SAndrey Smetanin 		*pdata = 0;
2345c919412SAndrey Smetanin 		break;
2355c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
2365c919412SAndrey Smetanin 		*pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
2375c919412SAndrey Smetanin 		break;
2385c919412SAndrey Smetanin 	default:
2395c919412SAndrey Smetanin 		ret = 1;
2405c919412SAndrey Smetanin 		break;
2415c919412SAndrey Smetanin 	}
2425c919412SAndrey Smetanin 	return ret;
2435c919412SAndrey Smetanin }
2445c919412SAndrey Smetanin 
2455c919412SAndrey Smetanin int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
2465c919412SAndrey Smetanin {
2475c919412SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
2485c919412SAndrey Smetanin 	struct kvm_lapic_irq irq;
2495c919412SAndrey Smetanin 	int ret, vector;
2505c919412SAndrey Smetanin 
2515c919412SAndrey Smetanin 	if (sint >= ARRAY_SIZE(synic->sint))
2525c919412SAndrey Smetanin 		return -EINVAL;
2535c919412SAndrey Smetanin 
2545c919412SAndrey Smetanin 	vector = synic_get_sint_vector(synic_read_sint(synic, sint));
2555c919412SAndrey Smetanin 	if (vector < 0)
2565c919412SAndrey Smetanin 		return -ENOENT;
2575c919412SAndrey Smetanin 
2585c919412SAndrey Smetanin 	memset(&irq, 0, sizeof(irq));
2595c919412SAndrey Smetanin 	irq.dest_id = kvm_apic_id(vcpu->arch.apic);
2605c919412SAndrey Smetanin 	irq.dest_mode = APIC_DEST_PHYSICAL;
2615c919412SAndrey Smetanin 	irq.delivery_mode = APIC_DM_FIXED;
2625c919412SAndrey Smetanin 	irq.vector = vector;
2635c919412SAndrey Smetanin 	irq.level = 1;
2645c919412SAndrey Smetanin 
2655c919412SAndrey Smetanin 	ret = kvm_irq_delivery_to_apic(vcpu->kvm, NULL, &irq, NULL);
2665c919412SAndrey Smetanin 	vcpu_debug(vcpu, "Hyper-V SynIC set irq ret %d\n", ret);
2675c919412SAndrey Smetanin 	return ret;
2685c919412SAndrey Smetanin }
2695c919412SAndrey Smetanin 
2705c919412SAndrey Smetanin int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vcpu_id, u32 sint)
2715c919412SAndrey Smetanin {
2725c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic;
2735c919412SAndrey Smetanin 
2745c919412SAndrey Smetanin 	synic = synic_get(kvm, vcpu_id);
2755c919412SAndrey Smetanin 	if (!synic)
2765c919412SAndrey Smetanin 		return -EINVAL;
2775c919412SAndrey Smetanin 
2785c919412SAndrey Smetanin 	return synic_set_irq(synic, sint);
2795c919412SAndrey Smetanin }
2805c919412SAndrey Smetanin 
2815c919412SAndrey Smetanin void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
2825c919412SAndrey Smetanin {
2835c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
2845c919412SAndrey Smetanin 	int i;
2855c919412SAndrey Smetanin 
2865c919412SAndrey Smetanin 	vcpu_debug(vcpu, "Hyper-V SynIC send eoi vec %d\n", vector);
2875c919412SAndrey Smetanin 
2885c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
2895c919412SAndrey Smetanin 		if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
2905c919412SAndrey Smetanin 			kvm_hv_notify_acked_sint(vcpu, i);
2915c919412SAndrey Smetanin }
2925c919412SAndrey Smetanin 
2935c919412SAndrey Smetanin static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vcpu_id, u32 sint, int gsi)
2945c919412SAndrey Smetanin {
2955c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic;
2965c919412SAndrey Smetanin 
2975c919412SAndrey Smetanin 	synic = synic_get(kvm, vcpu_id);
2985c919412SAndrey Smetanin 	if (!synic)
2995c919412SAndrey Smetanin 		return -EINVAL;
3005c919412SAndrey Smetanin 
3015c919412SAndrey Smetanin 	if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
3025c919412SAndrey Smetanin 		return -EINVAL;
3035c919412SAndrey Smetanin 
3045c919412SAndrey Smetanin 	atomic_set(&synic->sint_to_gsi[sint], gsi);
3055c919412SAndrey Smetanin 	return 0;
3065c919412SAndrey Smetanin }
3075c919412SAndrey Smetanin 
3085c919412SAndrey Smetanin void kvm_hv_irq_routing_update(struct kvm *kvm)
3095c919412SAndrey Smetanin {
3105c919412SAndrey Smetanin 	struct kvm_irq_routing_table *irq_rt;
3115c919412SAndrey Smetanin 	struct kvm_kernel_irq_routing_entry *e;
3125c919412SAndrey Smetanin 	u32 gsi;
3135c919412SAndrey Smetanin 
3145c919412SAndrey Smetanin 	irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
3155c919412SAndrey Smetanin 					lockdep_is_held(&kvm->irq_lock));
3165c919412SAndrey Smetanin 
3175c919412SAndrey Smetanin 	for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
3185c919412SAndrey Smetanin 		hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
3195c919412SAndrey Smetanin 			if (e->type == KVM_IRQ_ROUTING_HV_SINT)
3205c919412SAndrey Smetanin 				kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
3215c919412SAndrey Smetanin 						    e->hv_sint.sint, gsi);
3225c919412SAndrey Smetanin 		}
3235c919412SAndrey Smetanin 	}
3245c919412SAndrey Smetanin }
3255c919412SAndrey Smetanin 
3265c919412SAndrey Smetanin static void synic_init(struct kvm_vcpu_hv_synic *synic)
3275c919412SAndrey Smetanin {
3285c919412SAndrey Smetanin 	int i;
3295c919412SAndrey Smetanin 
3305c919412SAndrey Smetanin 	memset(synic, 0, sizeof(*synic));
3315c919412SAndrey Smetanin 	synic->version = HV_SYNIC_VERSION_1;
3325c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
3335c919412SAndrey Smetanin 		atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
3345c919412SAndrey Smetanin 		atomic_set(&synic->sint_to_gsi[i], -1);
3355c919412SAndrey Smetanin 	}
3365c919412SAndrey Smetanin }
3375c919412SAndrey Smetanin 
3385c919412SAndrey Smetanin void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
3395c919412SAndrey Smetanin {
3405c919412SAndrey Smetanin 	synic_init(vcpu_to_synic(vcpu));
3415c919412SAndrey Smetanin }
3425c919412SAndrey Smetanin 
3435c919412SAndrey Smetanin int kvm_hv_activate_synic(struct kvm_vcpu *vcpu)
3445c919412SAndrey Smetanin {
3455c919412SAndrey Smetanin 	/*
3465c919412SAndrey Smetanin 	 * Hyper-V SynIC auto EOI SINT's are
3475c919412SAndrey Smetanin 	 * not compatible with APICV, so deactivate APICV
3485c919412SAndrey Smetanin 	 */
3495c919412SAndrey Smetanin 	kvm_vcpu_deactivate_apicv(vcpu);
3505c919412SAndrey Smetanin 	vcpu_to_synic(vcpu)->active = true;
3515c919412SAndrey Smetanin 	return 0;
3525c919412SAndrey Smetanin }
3535c919412SAndrey Smetanin 
354e83d5887SAndrey Smetanin static bool kvm_hv_msr_partition_wide(u32 msr)
355e83d5887SAndrey Smetanin {
356e83d5887SAndrey Smetanin 	bool r = false;
357e83d5887SAndrey Smetanin 
358e83d5887SAndrey Smetanin 	switch (msr) {
359e83d5887SAndrey Smetanin 	case HV_X64_MSR_GUEST_OS_ID:
360e83d5887SAndrey Smetanin 	case HV_X64_MSR_HYPERCALL:
361e83d5887SAndrey Smetanin 	case HV_X64_MSR_REFERENCE_TSC:
362e83d5887SAndrey Smetanin 	case HV_X64_MSR_TIME_REF_COUNT:
363e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_CTL:
364e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
365e516cebbSAndrey Smetanin 	case HV_X64_MSR_RESET:
366e83d5887SAndrey Smetanin 		r = true;
367e83d5887SAndrey Smetanin 		break;
368e83d5887SAndrey Smetanin 	}
369e83d5887SAndrey Smetanin 
370e83d5887SAndrey Smetanin 	return r;
371e83d5887SAndrey Smetanin }
372e83d5887SAndrey Smetanin 
373e7d9513bSAndrey Smetanin static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
374e7d9513bSAndrey Smetanin 				     u32 index, u64 *pdata)
375e7d9513bSAndrey Smetanin {
376e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
377e7d9513bSAndrey Smetanin 
378e7d9513bSAndrey Smetanin 	if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
379e7d9513bSAndrey Smetanin 		return -EINVAL;
380e7d9513bSAndrey Smetanin 
381e7d9513bSAndrey Smetanin 	*pdata = hv->hv_crash_param[index];
382e7d9513bSAndrey Smetanin 	return 0;
383e7d9513bSAndrey Smetanin }
384e7d9513bSAndrey Smetanin 
385e7d9513bSAndrey Smetanin static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata)
386e7d9513bSAndrey Smetanin {
387e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
388e7d9513bSAndrey Smetanin 
389e7d9513bSAndrey Smetanin 	*pdata = hv->hv_crash_ctl;
390e7d9513bSAndrey Smetanin 	return 0;
391e7d9513bSAndrey Smetanin }
392e7d9513bSAndrey Smetanin 
393e7d9513bSAndrey Smetanin static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host)
394e7d9513bSAndrey Smetanin {
395e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
396e7d9513bSAndrey Smetanin 
397e7d9513bSAndrey Smetanin 	if (host)
398e7d9513bSAndrey Smetanin 		hv->hv_crash_ctl = data & HV_X64_MSR_CRASH_CTL_NOTIFY;
399e7d9513bSAndrey Smetanin 
400e7d9513bSAndrey Smetanin 	if (!host && (data & HV_X64_MSR_CRASH_CTL_NOTIFY)) {
401e7d9513bSAndrey Smetanin 
402e7d9513bSAndrey Smetanin 		vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
403e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[0],
404e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[1],
405e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[2],
406e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[3],
407e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[4]);
408e7d9513bSAndrey Smetanin 
409e7d9513bSAndrey Smetanin 		/* Send notification about crash to user space */
410e7d9513bSAndrey Smetanin 		kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
411e7d9513bSAndrey Smetanin 	}
412e7d9513bSAndrey Smetanin 
413e7d9513bSAndrey Smetanin 	return 0;
414e7d9513bSAndrey Smetanin }
415e7d9513bSAndrey Smetanin 
416e7d9513bSAndrey Smetanin static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
417e7d9513bSAndrey Smetanin 				     u32 index, u64 data)
418e7d9513bSAndrey Smetanin {
419e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
420e7d9513bSAndrey Smetanin 
421e7d9513bSAndrey Smetanin 	if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
422e7d9513bSAndrey Smetanin 		return -EINVAL;
423e7d9513bSAndrey Smetanin 
424e7d9513bSAndrey Smetanin 	hv->hv_crash_param[index] = data;
425e7d9513bSAndrey Smetanin 	return 0;
426e7d9513bSAndrey Smetanin }
427e7d9513bSAndrey Smetanin 
428e7d9513bSAndrey Smetanin static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
429e7d9513bSAndrey Smetanin 			     bool host)
430e83d5887SAndrey Smetanin {
431e83d5887SAndrey Smetanin 	struct kvm *kvm = vcpu->kvm;
432e83d5887SAndrey Smetanin 	struct kvm_hv *hv = &kvm->arch.hyperv;
433e83d5887SAndrey Smetanin 
434e83d5887SAndrey Smetanin 	switch (msr) {
435e83d5887SAndrey Smetanin 	case HV_X64_MSR_GUEST_OS_ID:
436e83d5887SAndrey Smetanin 		hv->hv_guest_os_id = data;
437e83d5887SAndrey Smetanin 		/* setting guest os id to zero disables hypercall page */
438e83d5887SAndrey Smetanin 		if (!hv->hv_guest_os_id)
439e83d5887SAndrey Smetanin 			hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
440e83d5887SAndrey Smetanin 		break;
441e83d5887SAndrey Smetanin 	case HV_X64_MSR_HYPERCALL: {
442e83d5887SAndrey Smetanin 		u64 gfn;
443e83d5887SAndrey Smetanin 		unsigned long addr;
444e83d5887SAndrey Smetanin 		u8 instructions[4];
445e83d5887SAndrey Smetanin 
446e83d5887SAndrey Smetanin 		/* if guest os id is not set hypercall should remain disabled */
447e83d5887SAndrey Smetanin 		if (!hv->hv_guest_os_id)
448e83d5887SAndrey Smetanin 			break;
449e83d5887SAndrey Smetanin 		if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
450e83d5887SAndrey Smetanin 			hv->hv_hypercall = data;
451e83d5887SAndrey Smetanin 			break;
452e83d5887SAndrey Smetanin 		}
453e83d5887SAndrey Smetanin 		gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
454e83d5887SAndrey Smetanin 		addr = gfn_to_hva(kvm, gfn);
455e83d5887SAndrey Smetanin 		if (kvm_is_error_hva(addr))
456e83d5887SAndrey Smetanin 			return 1;
457e83d5887SAndrey Smetanin 		kvm_x86_ops->patch_hypercall(vcpu, instructions);
458e83d5887SAndrey Smetanin 		((unsigned char *)instructions)[3] = 0xc3; /* ret */
459e83d5887SAndrey Smetanin 		if (__copy_to_user((void __user *)addr, instructions, 4))
460e83d5887SAndrey Smetanin 			return 1;
461e83d5887SAndrey Smetanin 		hv->hv_hypercall = data;
462e83d5887SAndrey Smetanin 		mark_page_dirty(kvm, gfn);
463e83d5887SAndrey Smetanin 		break;
464e83d5887SAndrey Smetanin 	}
465e83d5887SAndrey Smetanin 	case HV_X64_MSR_REFERENCE_TSC: {
466e83d5887SAndrey Smetanin 		u64 gfn;
467e83d5887SAndrey Smetanin 		HV_REFERENCE_TSC_PAGE tsc_ref;
468e83d5887SAndrey Smetanin 
469e83d5887SAndrey Smetanin 		memset(&tsc_ref, 0, sizeof(tsc_ref));
470e83d5887SAndrey Smetanin 		hv->hv_tsc_page = data;
471e83d5887SAndrey Smetanin 		if (!(data & HV_X64_MSR_TSC_REFERENCE_ENABLE))
472e83d5887SAndrey Smetanin 			break;
473e83d5887SAndrey Smetanin 		gfn = data >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
474e83d5887SAndrey Smetanin 		if (kvm_write_guest(
475e83d5887SAndrey Smetanin 				kvm,
476e83d5887SAndrey Smetanin 				gfn << HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT,
477e83d5887SAndrey Smetanin 				&tsc_ref, sizeof(tsc_ref)))
478e83d5887SAndrey Smetanin 			return 1;
479e83d5887SAndrey Smetanin 		mark_page_dirty(kvm, gfn);
480e83d5887SAndrey Smetanin 		break;
481e83d5887SAndrey Smetanin 	}
482e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
483e7d9513bSAndrey Smetanin 		return kvm_hv_msr_set_crash_data(vcpu,
484e7d9513bSAndrey Smetanin 						 msr - HV_X64_MSR_CRASH_P0,
485e7d9513bSAndrey Smetanin 						 data);
486e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_CTL:
487e7d9513bSAndrey Smetanin 		return kvm_hv_msr_set_crash_ctl(vcpu, data, host);
488e516cebbSAndrey Smetanin 	case HV_X64_MSR_RESET:
489e516cebbSAndrey Smetanin 		if (data == 1) {
490e516cebbSAndrey Smetanin 			vcpu_debug(vcpu, "hyper-v reset requested\n");
491e516cebbSAndrey Smetanin 			kvm_make_request(KVM_REQ_HV_RESET, vcpu);
492e516cebbSAndrey Smetanin 		}
493e516cebbSAndrey Smetanin 		break;
494e83d5887SAndrey Smetanin 	default:
495e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
496e83d5887SAndrey Smetanin 			    msr, data);
497e83d5887SAndrey Smetanin 		return 1;
498e83d5887SAndrey Smetanin 	}
499e83d5887SAndrey Smetanin 	return 0;
500e83d5887SAndrey Smetanin }
501e83d5887SAndrey Smetanin 
5029eec50b8SAndrey Smetanin /* Calculate cpu time spent by current task in 100ns units */
5039eec50b8SAndrey Smetanin static u64 current_task_runtime_100ns(void)
5049eec50b8SAndrey Smetanin {
5059eec50b8SAndrey Smetanin 	cputime_t utime, stime;
5069eec50b8SAndrey Smetanin 
5079eec50b8SAndrey Smetanin 	task_cputime_adjusted(current, &utime, &stime);
5089eec50b8SAndrey Smetanin 	return div_u64(cputime_to_nsecs(utime + stime), 100);
5099eec50b8SAndrey Smetanin }
5109eec50b8SAndrey Smetanin 
5119eec50b8SAndrey Smetanin static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
512e83d5887SAndrey Smetanin {
513e83d5887SAndrey Smetanin 	struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
514e83d5887SAndrey Smetanin 
515e83d5887SAndrey Smetanin 	switch (msr) {
516e83d5887SAndrey Smetanin 	case HV_X64_MSR_APIC_ASSIST_PAGE: {
517e83d5887SAndrey Smetanin 		u64 gfn;
518e83d5887SAndrey Smetanin 		unsigned long addr;
519e83d5887SAndrey Smetanin 
520e83d5887SAndrey Smetanin 		if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
521e83d5887SAndrey Smetanin 			hv->hv_vapic = data;
522e83d5887SAndrey Smetanin 			if (kvm_lapic_enable_pv_eoi(vcpu, 0))
523e83d5887SAndrey Smetanin 				return 1;
524e83d5887SAndrey Smetanin 			break;
525e83d5887SAndrey Smetanin 		}
526e83d5887SAndrey Smetanin 		gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT;
527e83d5887SAndrey Smetanin 		addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
528e83d5887SAndrey Smetanin 		if (kvm_is_error_hva(addr))
529e83d5887SAndrey Smetanin 			return 1;
530e83d5887SAndrey Smetanin 		if (__clear_user((void __user *)addr, PAGE_SIZE))
531e83d5887SAndrey Smetanin 			return 1;
532e83d5887SAndrey Smetanin 		hv->hv_vapic = data;
533e83d5887SAndrey Smetanin 		kvm_vcpu_mark_page_dirty(vcpu, gfn);
534e83d5887SAndrey Smetanin 		if (kvm_lapic_enable_pv_eoi(vcpu,
535e83d5887SAndrey Smetanin 					    gfn_to_gpa(gfn) | KVM_MSR_ENABLED))
536e83d5887SAndrey Smetanin 			return 1;
537e83d5887SAndrey Smetanin 		break;
538e83d5887SAndrey Smetanin 	}
539e83d5887SAndrey Smetanin 	case HV_X64_MSR_EOI:
540e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
541e83d5887SAndrey Smetanin 	case HV_X64_MSR_ICR:
542e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
543e83d5887SAndrey Smetanin 	case HV_X64_MSR_TPR:
544e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
5459eec50b8SAndrey Smetanin 	case HV_X64_MSR_VP_RUNTIME:
5469eec50b8SAndrey Smetanin 		if (!host)
5479eec50b8SAndrey Smetanin 			return 1;
5489eec50b8SAndrey Smetanin 		hv->runtime_offset = data - current_task_runtime_100ns();
5499eec50b8SAndrey Smetanin 		break;
5505c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
5515c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
5525c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
5535c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
5545c919412SAndrey Smetanin 	case HV_X64_MSR_EOM:
5555c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
5565c919412SAndrey Smetanin 		return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host);
557e83d5887SAndrey Smetanin 	default:
558e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
559e83d5887SAndrey Smetanin 			    msr, data);
560e83d5887SAndrey Smetanin 		return 1;
561e83d5887SAndrey Smetanin 	}
562e83d5887SAndrey Smetanin 
563e83d5887SAndrey Smetanin 	return 0;
564e83d5887SAndrey Smetanin }
565e83d5887SAndrey Smetanin 
566e83d5887SAndrey Smetanin static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
567e83d5887SAndrey Smetanin {
568e83d5887SAndrey Smetanin 	u64 data = 0;
569e83d5887SAndrey Smetanin 	struct kvm *kvm = vcpu->kvm;
570e83d5887SAndrey Smetanin 	struct kvm_hv *hv = &kvm->arch.hyperv;
571e83d5887SAndrey Smetanin 
572e83d5887SAndrey Smetanin 	switch (msr) {
573e83d5887SAndrey Smetanin 	case HV_X64_MSR_GUEST_OS_ID:
574e83d5887SAndrey Smetanin 		data = hv->hv_guest_os_id;
575e83d5887SAndrey Smetanin 		break;
576e83d5887SAndrey Smetanin 	case HV_X64_MSR_HYPERCALL:
577e83d5887SAndrey Smetanin 		data = hv->hv_hypercall;
578e83d5887SAndrey Smetanin 		break;
579e83d5887SAndrey Smetanin 	case HV_X64_MSR_TIME_REF_COUNT: {
580e83d5887SAndrey Smetanin 		data =
581e83d5887SAndrey Smetanin 		     div_u64(get_kernel_ns() + kvm->arch.kvmclock_offset, 100);
582e83d5887SAndrey Smetanin 		break;
583e83d5887SAndrey Smetanin 	}
584e83d5887SAndrey Smetanin 	case HV_X64_MSR_REFERENCE_TSC:
585e83d5887SAndrey Smetanin 		data = hv->hv_tsc_page;
586e83d5887SAndrey Smetanin 		break;
587e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
588e7d9513bSAndrey Smetanin 		return kvm_hv_msr_get_crash_data(vcpu,
589e7d9513bSAndrey Smetanin 						 msr - HV_X64_MSR_CRASH_P0,
590e7d9513bSAndrey Smetanin 						 pdata);
591e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_CTL:
592e7d9513bSAndrey Smetanin 		return kvm_hv_msr_get_crash_ctl(vcpu, pdata);
593e516cebbSAndrey Smetanin 	case HV_X64_MSR_RESET:
594e516cebbSAndrey Smetanin 		data = 0;
595e516cebbSAndrey Smetanin 		break;
596e83d5887SAndrey Smetanin 	default:
597e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
598e83d5887SAndrey Smetanin 		return 1;
599e83d5887SAndrey Smetanin 	}
600e83d5887SAndrey Smetanin 
601e83d5887SAndrey Smetanin 	*pdata = data;
602e83d5887SAndrey Smetanin 	return 0;
603e83d5887SAndrey Smetanin }
604e83d5887SAndrey Smetanin 
605e83d5887SAndrey Smetanin static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
606e83d5887SAndrey Smetanin {
607e83d5887SAndrey Smetanin 	u64 data = 0;
608e83d5887SAndrey Smetanin 	struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
609e83d5887SAndrey Smetanin 
610e83d5887SAndrey Smetanin 	switch (msr) {
611e83d5887SAndrey Smetanin 	case HV_X64_MSR_VP_INDEX: {
612e83d5887SAndrey Smetanin 		int r;
613e83d5887SAndrey Smetanin 		struct kvm_vcpu *v;
614e83d5887SAndrey Smetanin 
615e83d5887SAndrey Smetanin 		kvm_for_each_vcpu(r, v, vcpu->kvm) {
616e83d5887SAndrey Smetanin 			if (v == vcpu) {
617e83d5887SAndrey Smetanin 				data = r;
618e83d5887SAndrey Smetanin 				break;
619e83d5887SAndrey Smetanin 			}
620e83d5887SAndrey Smetanin 		}
621e83d5887SAndrey Smetanin 		break;
622e83d5887SAndrey Smetanin 	}
623e83d5887SAndrey Smetanin 	case HV_X64_MSR_EOI:
624e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
625e83d5887SAndrey Smetanin 	case HV_X64_MSR_ICR:
626e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
627e83d5887SAndrey Smetanin 	case HV_X64_MSR_TPR:
628e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
629e83d5887SAndrey Smetanin 	case HV_X64_MSR_APIC_ASSIST_PAGE:
630e83d5887SAndrey Smetanin 		data = hv->hv_vapic;
631e83d5887SAndrey Smetanin 		break;
6329eec50b8SAndrey Smetanin 	case HV_X64_MSR_VP_RUNTIME:
6339eec50b8SAndrey Smetanin 		data = current_task_runtime_100ns() + hv->runtime_offset;
6349eec50b8SAndrey Smetanin 		break;
6355c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
6365c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
6375c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
6385c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
6395c919412SAndrey Smetanin 	case HV_X64_MSR_EOM:
6405c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
6415c919412SAndrey Smetanin 		return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata);
642e83d5887SAndrey Smetanin 	default:
643e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
644e83d5887SAndrey Smetanin 		return 1;
645e83d5887SAndrey Smetanin 	}
646e83d5887SAndrey Smetanin 	*pdata = data;
647e83d5887SAndrey Smetanin 	return 0;
648e83d5887SAndrey Smetanin }
649e83d5887SAndrey Smetanin 
650e7d9513bSAndrey Smetanin int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
651e83d5887SAndrey Smetanin {
652e83d5887SAndrey Smetanin 	if (kvm_hv_msr_partition_wide(msr)) {
653e83d5887SAndrey Smetanin 		int r;
654e83d5887SAndrey Smetanin 
655e83d5887SAndrey Smetanin 		mutex_lock(&vcpu->kvm->lock);
656e7d9513bSAndrey Smetanin 		r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
657e83d5887SAndrey Smetanin 		mutex_unlock(&vcpu->kvm->lock);
658e83d5887SAndrey Smetanin 		return r;
659e83d5887SAndrey Smetanin 	} else
6609eec50b8SAndrey Smetanin 		return kvm_hv_set_msr(vcpu, msr, data, host);
661e83d5887SAndrey Smetanin }
662e83d5887SAndrey Smetanin 
663e83d5887SAndrey Smetanin int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
664e83d5887SAndrey Smetanin {
665e83d5887SAndrey Smetanin 	if (kvm_hv_msr_partition_wide(msr)) {
666e83d5887SAndrey Smetanin 		int r;
667e83d5887SAndrey Smetanin 
668e83d5887SAndrey Smetanin 		mutex_lock(&vcpu->kvm->lock);
669e83d5887SAndrey Smetanin 		r = kvm_hv_get_msr_pw(vcpu, msr, pdata);
670e83d5887SAndrey Smetanin 		mutex_unlock(&vcpu->kvm->lock);
671e83d5887SAndrey Smetanin 		return r;
672e83d5887SAndrey Smetanin 	} else
673e83d5887SAndrey Smetanin 		return kvm_hv_get_msr(vcpu, msr, pdata);
674e83d5887SAndrey Smetanin }
675e83d5887SAndrey Smetanin 
676e83d5887SAndrey Smetanin bool kvm_hv_hypercall_enabled(struct kvm *kvm)
677e83d5887SAndrey Smetanin {
678e83d5887SAndrey Smetanin 	return kvm->arch.hyperv.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
679e83d5887SAndrey Smetanin }
680e83d5887SAndrey Smetanin 
681e83d5887SAndrey Smetanin int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
682e83d5887SAndrey Smetanin {
683e83d5887SAndrey Smetanin 	u64 param, ingpa, outgpa, ret;
684e83d5887SAndrey Smetanin 	uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
685e83d5887SAndrey Smetanin 	bool fast, longmode;
686e83d5887SAndrey Smetanin 
687e83d5887SAndrey Smetanin 	/*
688e83d5887SAndrey Smetanin 	 * hypercall generates UD from non zero cpl and real mode
689e83d5887SAndrey Smetanin 	 * per HYPER-V spec
690e83d5887SAndrey Smetanin 	 */
691e83d5887SAndrey Smetanin 	if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
692e83d5887SAndrey Smetanin 		kvm_queue_exception(vcpu, UD_VECTOR);
693e83d5887SAndrey Smetanin 		return 0;
694e83d5887SAndrey Smetanin 	}
695e83d5887SAndrey Smetanin 
696e83d5887SAndrey Smetanin 	longmode = is_64_bit_mode(vcpu);
697e83d5887SAndrey Smetanin 
698e83d5887SAndrey Smetanin 	if (!longmode) {
699e83d5887SAndrey Smetanin 		param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
700e83d5887SAndrey Smetanin 			(kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
701e83d5887SAndrey Smetanin 		ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
702e83d5887SAndrey Smetanin 			(kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
703e83d5887SAndrey Smetanin 		outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
704e83d5887SAndrey Smetanin 			(kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
705e83d5887SAndrey Smetanin 	}
706e83d5887SAndrey Smetanin #ifdef CONFIG_X86_64
707e83d5887SAndrey Smetanin 	else {
708e83d5887SAndrey Smetanin 		param = kvm_register_read(vcpu, VCPU_REGS_RCX);
709e83d5887SAndrey Smetanin 		ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
710e83d5887SAndrey Smetanin 		outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
711e83d5887SAndrey Smetanin 	}
712e83d5887SAndrey Smetanin #endif
713e83d5887SAndrey Smetanin 
714e83d5887SAndrey Smetanin 	code = param & 0xffff;
715e83d5887SAndrey Smetanin 	fast = (param >> 16) & 0x1;
716e83d5887SAndrey Smetanin 	rep_cnt = (param >> 32) & 0xfff;
717e83d5887SAndrey Smetanin 	rep_idx = (param >> 48) & 0xfff;
718e83d5887SAndrey Smetanin 
719e83d5887SAndrey Smetanin 	trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
720e83d5887SAndrey Smetanin 
721e83d5887SAndrey Smetanin 	switch (code) {
722e83d5887SAndrey Smetanin 	case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
723e83d5887SAndrey Smetanin 		kvm_vcpu_on_spin(vcpu);
724e83d5887SAndrey Smetanin 		break;
725e83d5887SAndrey Smetanin 	default:
726e83d5887SAndrey Smetanin 		res = HV_STATUS_INVALID_HYPERCALL_CODE;
727e83d5887SAndrey Smetanin 		break;
728e83d5887SAndrey Smetanin 	}
729e83d5887SAndrey Smetanin 
730e83d5887SAndrey Smetanin 	ret = res | (((u64)rep_done & 0xfff) << 32);
731e83d5887SAndrey Smetanin 	if (longmode) {
732e83d5887SAndrey Smetanin 		kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
733e83d5887SAndrey Smetanin 	} else {
734e83d5887SAndrey Smetanin 		kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
735e83d5887SAndrey Smetanin 		kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
736e83d5887SAndrey Smetanin 	}
737e83d5887SAndrey Smetanin 
738e83d5887SAndrey Smetanin 	return 1;
739e83d5887SAndrey Smetanin }
740