xref: /openbmc/linux/arch/x86/kvm/hyperv.c (revision 5c919412fe61c35947816fdbd5f7bd09fe0dd073)
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"
26*5c919412SAndrey Smetanin #include "ioapic.h"
27e83d5887SAndrey Smetanin #include "hyperv.h"
28e83d5887SAndrey Smetanin 
29e83d5887SAndrey Smetanin #include <linux/kvm_host.h>
30*5c919412SAndrey Smetanin #include <asm/apicdef.h>
31e83d5887SAndrey Smetanin #include <trace/events/kvm.h>
32e83d5887SAndrey Smetanin 
33e83d5887SAndrey Smetanin #include "trace.h"
34e83d5887SAndrey Smetanin 
35*5c919412SAndrey Smetanin static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
36*5c919412SAndrey Smetanin {
37*5c919412SAndrey Smetanin 	return atomic64_read(&synic->sint[sint]);
38*5c919412SAndrey Smetanin }
39*5c919412SAndrey Smetanin 
40*5c919412SAndrey Smetanin static inline int synic_get_sint_vector(u64 sint_value)
41*5c919412SAndrey Smetanin {
42*5c919412SAndrey Smetanin 	if (sint_value & HV_SYNIC_SINT_MASKED)
43*5c919412SAndrey Smetanin 		return -1;
44*5c919412SAndrey Smetanin 	return sint_value & HV_SYNIC_SINT_VECTOR_MASK;
45*5c919412SAndrey Smetanin }
46*5c919412SAndrey Smetanin 
47*5c919412SAndrey Smetanin static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic,
48*5c919412SAndrey Smetanin 				      int vector)
49*5c919412SAndrey Smetanin {
50*5c919412SAndrey Smetanin 	int i;
51*5c919412SAndrey Smetanin 
52*5c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
53*5c919412SAndrey Smetanin 		if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
54*5c919412SAndrey Smetanin 			return true;
55*5c919412SAndrey Smetanin 	}
56*5c919412SAndrey Smetanin 	return false;
57*5c919412SAndrey Smetanin }
58*5c919412SAndrey Smetanin 
59*5c919412SAndrey Smetanin static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic,
60*5c919412SAndrey Smetanin 				     int vector)
61*5c919412SAndrey Smetanin {
62*5c919412SAndrey Smetanin 	int i;
63*5c919412SAndrey Smetanin 	u64 sint_value;
64*5c919412SAndrey Smetanin 
65*5c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
66*5c919412SAndrey Smetanin 		sint_value = synic_read_sint(synic, i);
67*5c919412SAndrey Smetanin 		if (synic_get_sint_vector(sint_value) == vector &&
68*5c919412SAndrey Smetanin 		    sint_value & HV_SYNIC_SINT_AUTO_EOI)
69*5c919412SAndrey Smetanin 			return true;
70*5c919412SAndrey Smetanin 	}
71*5c919412SAndrey Smetanin 	return false;
72*5c919412SAndrey Smetanin }
73*5c919412SAndrey Smetanin 
74*5c919412SAndrey Smetanin static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint, u64 data)
75*5c919412SAndrey Smetanin {
76*5c919412SAndrey Smetanin 	int vector;
77*5c919412SAndrey Smetanin 
78*5c919412SAndrey Smetanin 	vector = data & HV_SYNIC_SINT_VECTOR_MASK;
79*5c919412SAndrey Smetanin 	if (vector < 16)
80*5c919412SAndrey Smetanin 		return 1;
81*5c919412SAndrey Smetanin 	/*
82*5c919412SAndrey Smetanin 	 * Guest may configure multiple SINTs to use the same vector, so
83*5c919412SAndrey Smetanin 	 * we maintain a bitmap of vectors handled by synic, and a
84*5c919412SAndrey Smetanin 	 * bitmap of vectors with auto-eoi behavior.  The bitmaps are
85*5c919412SAndrey Smetanin 	 * updated here, and atomically queried on fast paths.
86*5c919412SAndrey Smetanin 	 */
87*5c919412SAndrey Smetanin 
88*5c919412SAndrey Smetanin 	atomic64_set(&synic->sint[sint], data);
89*5c919412SAndrey Smetanin 
90*5c919412SAndrey Smetanin 	if (synic_has_vector_connected(synic, vector))
91*5c919412SAndrey Smetanin 		__set_bit(vector, synic->vec_bitmap);
92*5c919412SAndrey Smetanin 	else
93*5c919412SAndrey Smetanin 		__clear_bit(vector, synic->vec_bitmap);
94*5c919412SAndrey Smetanin 
95*5c919412SAndrey Smetanin 	if (synic_has_vector_auto_eoi(synic, vector))
96*5c919412SAndrey Smetanin 		__set_bit(vector, synic->auto_eoi_bitmap);
97*5c919412SAndrey Smetanin 	else
98*5c919412SAndrey Smetanin 		__clear_bit(vector, synic->auto_eoi_bitmap);
99*5c919412SAndrey Smetanin 
100*5c919412SAndrey Smetanin 	/* Load SynIC vectors into EOI exit bitmap */
101*5c919412SAndrey Smetanin 	kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic));
102*5c919412SAndrey Smetanin 	return 0;
103*5c919412SAndrey Smetanin }
104*5c919412SAndrey Smetanin 
105*5c919412SAndrey Smetanin static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vcpu_id)
106*5c919412SAndrey Smetanin {
107*5c919412SAndrey Smetanin 	struct kvm_vcpu *vcpu;
108*5c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic;
109*5c919412SAndrey Smetanin 
110*5c919412SAndrey Smetanin 	if (vcpu_id >= atomic_read(&kvm->online_vcpus))
111*5c919412SAndrey Smetanin 		return NULL;
112*5c919412SAndrey Smetanin 	vcpu = kvm_get_vcpu(kvm, vcpu_id);
113*5c919412SAndrey Smetanin 	if (!vcpu)
114*5c919412SAndrey Smetanin 		return NULL;
115*5c919412SAndrey Smetanin 	synic = vcpu_to_synic(vcpu);
116*5c919412SAndrey Smetanin 	return (synic->active) ? synic : NULL;
117*5c919412SAndrey Smetanin }
118*5c919412SAndrey Smetanin 
119*5c919412SAndrey Smetanin static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
120*5c919412SAndrey Smetanin {
121*5c919412SAndrey Smetanin 	struct kvm *kvm = vcpu->kvm;
122*5c919412SAndrey Smetanin 	int gsi, idx;
123*5c919412SAndrey Smetanin 
124*5c919412SAndrey Smetanin 	vcpu_debug(vcpu, "Hyper-V SynIC acked sint %d\n", sint);
125*5c919412SAndrey Smetanin 
126*5c919412SAndrey Smetanin 	idx = srcu_read_lock(&kvm->irq_srcu);
127*5c919412SAndrey Smetanin 	gsi = atomic_read(&vcpu_to_synic(vcpu)->sint_to_gsi[sint]);
128*5c919412SAndrey Smetanin 	if (gsi != -1)
129*5c919412SAndrey Smetanin 		kvm_notify_acked_gsi(kvm, gsi);
130*5c919412SAndrey Smetanin 	srcu_read_unlock(&kvm->irq_srcu, idx);
131*5c919412SAndrey Smetanin }
132*5c919412SAndrey Smetanin 
133*5c919412SAndrey Smetanin static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
134*5c919412SAndrey Smetanin 			 u32 msr, u64 data, bool host)
135*5c919412SAndrey Smetanin {
136*5c919412SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
137*5c919412SAndrey Smetanin 	int ret;
138*5c919412SAndrey Smetanin 
139*5c919412SAndrey Smetanin 	if (!synic->active)
140*5c919412SAndrey Smetanin 		return 1;
141*5c919412SAndrey Smetanin 
142*5c919412SAndrey Smetanin 	vcpu_debug(vcpu, "Hyper-V SynIC set msr 0x%x 0x%llx host %d\n",
143*5c919412SAndrey Smetanin 		   msr, data, host);
144*5c919412SAndrey Smetanin 	ret = 0;
145*5c919412SAndrey Smetanin 	switch (msr) {
146*5c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
147*5c919412SAndrey Smetanin 		synic->control = data;
148*5c919412SAndrey Smetanin 		break;
149*5c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
150*5c919412SAndrey Smetanin 		if (!host) {
151*5c919412SAndrey Smetanin 			ret = 1;
152*5c919412SAndrey Smetanin 			break;
153*5c919412SAndrey Smetanin 		}
154*5c919412SAndrey Smetanin 		synic->version = data;
155*5c919412SAndrey Smetanin 		break;
156*5c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
157*5c919412SAndrey Smetanin 		if (data & HV_SYNIC_SIEFP_ENABLE)
158*5c919412SAndrey Smetanin 			if (kvm_clear_guest(vcpu->kvm,
159*5c919412SAndrey Smetanin 					    data & PAGE_MASK, PAGE_SIZE)) {
160*5c919412SAndrey Smetanin 				ret = 1;
161*5c919412SAndrey Smetanin 				break;
162*5c919412SAndrey Smetanin 			}
163*5c919412SAndrey Smetanin 		synic->evt_page = data;
164*5c919412SAndrey Smetanin 		break;
165*5c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
166*5c919412SAndrey Smetanin 		if (data & HV_SYNIC_SIMP_ENABLE)
167*5c919412SAndrey Smetanin 			if (kvm_clear_guest(vcpu->kvm,
168*5c919412SAndrey Smetanin 					    data & PAGE_MASK, PAGE_SIZE)) {
169*5c919412SAndrey Smetanin 				ret = 1;
170*5c919412SAndrey Smetanin 				break;
171*5c919412SAndrey Smetanin 			}
172*5c919412SAndrey Smetanin 		synic->msg_page = data;
173*5c919412SAndrey Smetanin 		break;
174*5c919412SAndrey Smetanin 	case HV_X64_MSR_EOM: {
175*5c919412SAndrey Smetanin 		int i;
176*5c919412SAndrey Smetanin 
177*5c919412SAndrey Smetanin 		for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
178*5c919412SAndrey Smetanin 			kvm_hv_notify_acked_sint(vcpu, i);
179*5c919412SAndrey Smetanin 		break;
180*5c919412SAndrey Smetanin 	}
181*5c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
182*5c919412SAndrey Smetanin 		ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data);
183*5c919412SAndrey Smetanin 		break;
184*5c919412SAndrey Smetanin 	default:
185*5c919412SAndrey Smetanin 		ret = 1;
186*5c919412SAndrey Smetanin 		break;
187*5c919412SAndrey Smetanin 	}
188*5c919412SAndrey Smetanin 	return ret;
189*5c919412SAndrey Smetanin }
190*5c919412SAndrey Smetanin 
191*5c919412SAndrey Smetanin static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata)
192*5c919412SAndrey Smetanin {
193*5c919412SAndrey Smetanin 	int ret;
194*5c919412SAndrey Smetanin 
195*5c919412SAndrey Smetanin 	if (!synic->active)
196*5c919412SAndrey Smetanin 		return 1;
197*5c919412SAndrey Smetanin 
198*5c919412SAndrey Smetanin 	ret = 0;
199*5c919412SAndrey Smetanin 	switch (msr) {
200*5c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
201*5c919412SAndrey Smetanin 		*pdata = synic->control;
202*5c919412SAndrey Smetanin 		break;
203*5c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
204*5c919412SAndrey Smetanin 		*pdata = synic->version;
205*5c919412SAndrey Smetanin 		break;
206*5c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
207*5c919412SAndrey Smetanin 		*pdata = synic->evt_page;
208*5c919412SAndrey Smetanin 		break;
209*5c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
210*5c919412SAndrey Smetanin 		*pdata = synic->msg_page;
211*5c919412SAndrey Smetanin 		break;
212*5c919412SAndrey Smetanin 	case HV_X64_MSR_EOM:
213*5c919412SAndrey Smetanin 		*pdata = 0;
214*5c919412SAndrey Smetanin 		break;
215*5c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
216*5c919412SAndrey Smetanin 		*pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
217*5c919412SAndrey Smetanin 		break;
218*5c919412SAndrey Smetanin 	default:
219*5c919412SAndrey Smetanin 		ret = 1;
220*5c919412SAndrey Smetanin 		break;
221*5c919412SAndrey Smetanin 	}
222*5c919412SAndrey Smetanin 	return ret;
223*5c919412SAndrey Smetanin }
224*5c919412SAndrey Smetanin 
225*5c919412SAndrey Smetanin int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
226*5c919412SAndrey Smetanin {
227*5c919412SAndrey Smetanin 	struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
228*5c919412SAndrey Smetanin 	struct kvm_lapic_irq irq;
229*5c919412SAndrey Smetanin 	int ret, vector;
230*5c919412SAndrey Smetanin 
231*5c919412SAndrey Smetanin 	if (sint >= ARRAY_SIZE(synic->sint))
232*5c919412SAndrey Smetanin 		return -EINVAL;
233*5c919412SAndrey Smetanin 
234*5c919412SAndrey Smetanin 	vector = synic_get_sint_vector(synic_read_sint(synic, sint));
235*5c919412SAndrey Smetanin 	if (vector < 0)
236*5c919412SAndrey Smetanin 		return -ENOENT;
237*5c919412SAndrey Smetanin 
238*5c919412SAndrey Smetanin 	memset(&irq, 0, sizeof(irq));
239*5c919412SAndrey Smetanin 	irq.dest_id = kvm_apic_id(vcpu->arch.apic);
240*5c919412SAndrey Smetanin 	irq.dest_mode = APIC_DEST_PHYSICAL;
241*5c919412SAndrey Smetanin 	irq.delivery_mode = APIC_DM_FIXED;
242*5c919412SAndrey Smetanin 	irq.vector = vector;
243*5c919412SAndrey Smetanin 	irq.level = 1;
244*5c919412SAndrey Smetanin 
245*5c919412SAndrey Smetanin 	ret = kvm_irq_delivery_to_apic(vcpu->kvm, NULL, &irq, NULL);
246*5c919412SAndrey Smetanin 	vcpu_debug(vcpu, "Hyper-V SynIC set irq ret %d\n", ret);
247*5c919412SAndrey Smetanin 	return ret;
248*5c919412SAndrey Smetanin }
249*5c919412SAndrey Smetanin 
250*5c919412SAndrey Smetanin int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vcpu_id, u32 sint)
251*5c919412SAndrey Smetanin {
252*5c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic;
253*5c919412SAndrey Smetanin 
254*5c919412SAndrey Smetanin 	synic = synic_get(kvm, vcpu_id);
255*5c919412SAndrey Smetanin 	if (!synic)
256*5c919412SAndrey Smetanin 		return -EINVAL;
257*5c919412SAndrey Smetanin 
258*5c919412SAndrey Smetanin 	return synic_set_irq(synic, sint);
259*5c919412SAndrey Smetanin }
260*5c919412SAndrey Smetanin 
261*5c919412SAndrey Smetanin void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
262*5c919412SAndrey Smetanin {
263*5c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
264*5c919412SAndrey Smetanin 	int i;
265*5c919412SAndrey Smetanin 
266*5c919412SAndrey Smetanin 	vcpu_debug(vcpu, "Hyper-V SynIC send eoi vec %d\n", vector);
267*5c919412SAndrey Smetanin 
268*5c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
269*5c919412SAndrey Smetanin 		if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
270*5c919412SAndrey Smetanin 			kvm_hv_notify_acked_sint(vcpu, i);
271*5c919412SAndrey Smetanin }
272*5c919412SAndrey Smetanin 
273*5c919412SAndrey Smetanin static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vcpu_id, u32 sint, int gsi)
274*5c919412SAndrey Smetanin {
275*5c919412SAndrey Smetanin 	struct kvm_vcpu_hv_synic *synic;
276*5c919412SAndrey Smetanin 
277*5c919412SAndrey Smetanin 	synic = synic_get(kvm, vcpu_id);
278*5c919412SAndrey Smetanin 	if (!synic)
279*5c919412SAndrey Smetanin 		return -EINVAL;
280*5c919412SAndrey Smetanin 
281*5c919412SAndrey Smetanin 	if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
282*5c919412SAndrey Smetanin 		return -EINVAL;
283*5c919412SAndrey Smetanin 
284*5c919412SAndrey Smetanin 	atomic_set(&synic->sint_to_gsi[sint], gsi);
285*5c919412SAndrey Smetanin 	return 0;
286*5c919412SAndrey Smetanin }
287*5c919412SAndrey Smetanin 
288*5c919412SAndrey Smetanin void kvm_hv_irq_routing_update(struct kvm *kvm)
289*5c919412SAndrey Smetanin {
290*5c919412SAndrey Smetanin 	struct kvm_irq_routing_table *irq_rt;
291*5c919412SAndrey Smetanin 	struct kvm_kernel_irq_routing_entry *e;
292*5c919412SAndrey Smetanin 	u32 gsi;
293*5c919412SAndrey Smetanin 
294*5c919412SAndrey Smetanin 	irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
295*5c919412SAndrey Smetanin 					lockdep_is_held(&kvm->irq_lock));
296*5c919412SAndrey Smetanin 
297*5c919412SAndrey Smetanin 	for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
298*5c919412SAndrey Smetanin 		hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
299*5c919412SAndrey Smetanin 			if (e->type == KVM_IRQ_ROUTING_HV_SINT)
300*5c919412SAndrey Smetanin 				kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
301*5c919412SAndrey Smetanin 						    e->hv_sint.sint, gsi);
302*5c919412SAndrey Smetanin 		}
303*5c919412SAndrey Smetanin 	}
304*5c919412SAndrey Smetanin }
305*5c919412SAndrey Smetanin 
306*5c919412SAndrey Smetanin static void synic_init(struct kvm_vcpu_hv_synic *synic)
307*5c919412SAndrey Smetanin {
308*5c919412SAndrey Smetanin 	int i;
309*5c919412SAndrey Smetanin 
310*5c919412SAndrey Smetanin 	memset(synic, 0, sizeof(*synic));
311*5c919412SAndrey Smetanin 	synic->version = HV_SYNIC_VERSION_1;
312*5c919412SAndrey Smetanin 	for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
313*5c919412SAndrey Smetanin 		atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
314*5c919412SAndrey Smetanin 		atomic_set(&synic->sint_to_gsi[i], -1);
315*5c919412SAndrey Smetanin 	}
316*5c919412SAndrey Smetanin }
317*5c919412SAndrey Smetanin 
318*5c919412SAndrey Smetanin void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
319*5c919412SAndrey Smetanin {
320*5c919412SAndrey Smetanin 	synic_init(vcpu_to_synic(vcpu));
321*5c919412SAndrey Smetanin }
322*5c919412SAndrey Smetanin 
323*5c919412SAndrey Smetanin int kvm_hv_activate_synic(struct kvm_vcpu *vcpu)
324*5c919412SAndrey Smetanin {
325*5c919412SAndrey Smetanin 	/*
326*5c919412SAndrey Smetanin 	 * Hyper-V SynIC auto EOI SINT's are
327*5c919412SAndrey Smetanin 	 * not compatible with APICV, so deactivate APICV
328*5c919412SAndrey Smetanin 	 */
329*5c919412SAndrey Smetanin 	kvm_vcpu_deactivate_apicv(vcpu);
330*5c919412SAndrey Smetanin 	vcpu_to_synic(vcpu)->active = true;
331*5c919412SAndrey Smetanin 	return 0;
332*5c919412SAndrey Smetanin }
333*5c919412SAndrey Smetanin 
334e83d5887SAndrey Smetanin static bool kvm_hv_msr_partition_wide(u32 msr)
335e83d5887SAndrey Smetanin {
336e83d5887SAndrey Smetanin 	bool r = false;
337e83d5887SAndrey Smetanin 
338e83d5887SAndrey Smetanin 	switch (msr) {
339e83d5887SAndrey Smetanin 	case HV_X64_MSR_GUEST_OS_ID:
340e83d5887SAndrey Smetanin 	case HV_X64_MSR_HYPERCALL:
341e83d5887SAndrey Smetanin 	case HV_X64_MSR_REFERENCE_TSC:
342e83d5887SAndrey Smetanin 	case HV_X64_MSR_TIME_REF_COUNT:
343e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_CTL:
344e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
345e516cebbSAndrey Smetanin 	case HV_X64_MSR_RESET:
346e83d5887SAndrey Smetanin 		r = true;
347e83d5887SAndrey Smetanin 		break;
348e83d5887SAndrey Smetanin 	}
349e83d5887SAndrey Smetanin 
350e83d5887SAndrey Smetanin 	return r;
351e83d5887SAndrey Smetanin }
352e83d5887SAndrey Smetanin 
353e7d9513bSAndrey Smetanin static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
354e7d9513bSAndrey Smetanin 				     u32 index, u64 *pdata)
355e7d9513bSAndrey Smetanin {
356e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
357e7d9513bSAndrey Smetanin 
358e7d9513bSAndrey Smetanin 	if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
359e7d9513bSAndrey Smetanin 		return -EINVAL;
360e7d9513bSAndrey Smetanin 
361e7d9513bSAndrey Smetanin 	*pdata = hv->hv_crash_param[index];
362e7d9513bSAndrey Smetanin 	return 0;
363e7d9513bSAndrey Smetanin }
364e7d9513bSAndrey Smetanin 
365e7d9513bSAndrey Smetanin static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata)
366e7d9513bSAndrey Smetanin {
367e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
368e7d9513bSAndrey Smetanin 
369e7d9513bSAndrey Smetanin 	*pdata = hv->hv_crash_ctl;
370e7d9513bSAndrey Smetanin 	return 0;
371e7d9513bSAndrey Smetanin }
372e7d9513bSAndrey Smetanin 
373e7d9513bSAndrey Smetanin static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host)
374e7d9513bSAndrey Smetanin {
375e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
376e7d9513bSAndrey Smetanin 
377e7d9513bSAndrey Smetanin 	if (host)
378e7d9513bSAndrey Smetanin 		hv->hv_crash_ctl = data & HV_X64_MSR_CRASH_CTL_NOTIFY;
379e7d9513bSAndrey Smetanin 
380e7d9513bSAndrey Smetanin 	if (!host && (data & HV_X64_MSR_CRASH_CTL_NOTIFY)) {
381e7d9513bSAndrey Smetanin 
382e7d9513bSAndrey Smetanin 		vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
383e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[0],
384e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[1],
385e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[2],
386e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[3],
387e7d9513bSAndrey Smetanin 			  hv->hv_crash_param[4]);
388e7d9513bSAndrey Smetanin 
389e7d9513bSAndrey Smetanin 		/* Send notification about crash to user space */
390e7d9513bSAndrey Smetanin 		kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
391e7d9513bSAndrey Smetanin 	}
392e7d9513bSAndrey Smetanin 
393e7d9513bSAndrey Smetanin 	return 0;
394e7d9513bSAndrey Smetanin }
395e7d9513bSAndrey Smetanin 
396e7d9513bSAndrey Smetanin static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
397e7d9513bSAndrey Smetanin 				     u32 index, u64 data)
398e7d9513bSAndrey Smetanin {
399e7d9513bSAndrey Smetanin 	struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
400e7d9513bSAndrey Smetanin 
401e7d9513bSAndrey Smetanin 	if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
402e7d9513bSAndrey Smetanin 		return -EINVAL;
403e7d9513bSAndrey Smetanin 
404e7d9513bSAndrey Smetanin 	hv->hv_crash_param[index] = data;
405e7d9513bSAndrey Smetanin 	return 0;
406e7d9513bSAndrey Smetanin }
407e7d9513bSAndrey Smetanin 
408e7d9513bSAndrey Smetanin static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
409e7d9513bSAndrey Smetanin 			     bool host)
410e83d5887SAndrey Smetanin {
411e83d5887SAndrey Smetanin 	struct kvm *kvm = vcpu->kvm;
412e83d5887SAndrey Smetanin 	struct kvm_hv *hv = &kvm->arch.hyperv;
413e83d5887SAndrey Smetanin 
414e83d5887SAndrey Smetanin 	switch (msr) {
415e83d5887SAndrey Smetanin 	case HV_X64_MSR_GUEST_OS_ID:
416e83d5887SAndrey Smetanin 		hv->hv_guest_os_id = data;
417e83d5887SAndrey Smetanin 		/* setting guest os id to zero disables hypercall page */
418e83d5887SAndrey Smetanin 		if (!hv->hv_guest_os_id)
419e83d5887SAndrey Smetanin 			hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
420e83d5887SAndrey Smetanin 		break;
421e83d5887SAndrey Smetanin 	case HV_X64_MSR_HYPERCALL: {
422e83d5887SAndrey Smetanin 		u64 gfn;
423e83d5887SAndrey Smetanin 		unsigned long addr;
424e83d5887SAndrey Smetanin 		u8 instructions[4];
425e83d5887SAndrey Smetanin 
426e83d5887SAndrey Smetanin 		/* if guest os id is not set hypercall should remain disabled */
427e83d5887SAndrey Smetanin 		if (!hv->hv_guest_os_id)
428e83d5887SAndrey Smetanin 			break;
429e83d5887SAndrey Smetanin 		if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
430e83d5887SAndrey Smetanin 			hv->hv_hypercall = data;
431e83d5887SAndrey Smetanin 			break;
432e83d5887SAndrey Smetanin 		}
433e83d5887SAndrey Smetanin 		gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
434e83d5887SAndrey Smetanin 		addr = gfn_to_hva(kvm, gfn);
435e83d5887SAndrey Smetanin 		if (kvm_is_error_hva(addr))
436e83d5887SAndrey Smetanin 			return 1;
437e83d5887SAndrey Smetanin 		kvm_x86_ops->patch_hypercall(vcpu, instructions);
438e83d5887SAndrey Smetanin 		((unsigned char *)instructions)[3] = 0xc3; /* ret */
439e83d5887SAndrey Smetanin 		if (__copy_to_user((void __user *)addr, instructions, 4))
440e83d5887SAndrey Smetanin 			return 1;
441e83d5887SAndrey Smetanin 		hv->hv_hypercall = data;
442e83d5887SAndrey Smetanin 		mark_page_dirty(kvm, gfn);
443e83d5887SAndrey Smetanin 		break;
444e83d5887SAndrey Smetanin 	}
445e83d5887SAndrey Smetanin 	case HV_X64_MSR_REFERENCE_TSC: {
446e83d5887SAndrey Smetanin 		u64 gfn;
447e83d5887SAndrey Smetanin 		HV_REFERENCE_TSC_PAGE tsc_ref;
448e83d5887SAndrey Smetanin 
449e83d5887SAndrey Smetanin 		memset(&tsc_ref, 0, sizeof(tsc_ref));
450e83d5887SAndrey Smetanin 		hv->hv_tsc_page = data;
451e83d5887SAndrey Smetanin 		if (!(data & HV_X64_MSR_TSC_REFERENCE_ENABLE))
452e83d5887SAndrey Smetanin 			break;
453e83d5887SAndrey Smetanin 		gfn = data >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
454e83d5887SAndrey Smetanin 		if (kvm_write_guest(
455e83d5887SAndrey Smetanin 				kvm,
456e83d5887SAndrey Smetanin 				gfn << HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT,
457e83d5887SAndrey Smetanin 				&tsc_ref, sizeof(tsc_ref)))
458e83d5887SAndrey Smetanin 			return 1;
459e83d5887SAndrey Smetanin 		mark_page_dirty(kvm, gfn);
460e83d5887SAndrey Smetanin 		break;
461e83d5887SAndrey Smetanin 	}
462e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
463e7d9513bSAndrey Smetanin 		return kvm_hv_msr_set_crash_data(vcpu,
464e7d9513bSAndrey Smetanin 						 msr - HV_X64_MSR_CRASH_P0,
465e7d9513bSAndrey Smetanin 						 data);
466e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_CTL:
467e7d9513bSAndrey Smetanin 		return kvm_hv_msr_set_crash_ctl(vcpu, data, host);
468e516cebbSAndrey Smetanin 	case HV_X64_MSR_RESET:
469e516cebbSAndrey Smetanin 		if (data == 1) {
470e516cebbSAndrey Smetanin 			vcpu_debug(vcpu, "hyper-v reset requested\n");
471e516cebbSAndrey Smetanin 			kvm_make_request(KVM_REQ_HV_RESET, vcpu);
472e516cebbSAndrey Smetanin 		}
473e516cebbSAndrey Smetanin 		break;
474e83d5887SAndrey Smetanin 	default:
475e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
476e83d5887SAndrey Smetanin 			    msr, data);
477e83d5887SAndrey Smetanin 		return 1;
478e83d5887SAndrey Smetanin 	}
479e83d5887SAndrey Smetanin 	return 0;
480e83d5887SAndrey Smetanin }
481e83d5887SAndrey Smetanin 
4829eec50b8SAndrey Smetanin /* Calculate cpu time spent by current task in 100ns units */
4839eec50b8SAndrey Smetanin static u64 current_task_runtime_100ns(void)
4849eec50b8SAndrey Smetanin {
4859eec50b8SAndrey Smetanin 	cputime_t utime, stime;
4869eec50b8SAndrey Smetanin 
4879eec50b8SAndrey Smetanin 	task_cputime_adjusted(current, &utime, &stime);
4889eec50b8SAndrey Smetanin 	return div_u64(cputime_to_nsecs(utime + stime), 100);
4899eec50b8SAndrey Smetanin }
4909eec50b8SAndrey Smetanin 
4919eec50b8SAndrey Smetanin static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
492e83d5887SAndrey Smetanin {
493e83d5887SAndrey Smetanin 	struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
494e83d5887SAndrey Smetanin 
495e83d5887SAndrey Smetanin 	switch (msr) {
496e83d5887SAndrey Smetanin 	case HV_X64_MSR_APIC_ASSIST_PAGE: {
497e83d5887SAndrey Smetanin 		u64 gfn;
498e83d5887SAndrey Smetanin 		unsigned long addr;
499e83d5887SAndrey Smetanin 
500e83d5887SAndrey Smetanin 		if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
501e83d5887SAndrey Smetanin 			hv->hv_vapic = data;
502e83d5887SAndrey Smetanin 			if (kvm_lapic_enable_pv_eoi(vcpu, 0))
503e83d5887SAndrey Smetanin 				return 1;
504e83d5887SAndrey Smetanin 			break;
505e83d5887SAndrey Smetanin 		}
506e83d5887SAndrey Smetanin 		gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT;
507e83d5887SAndrey Smetanin 		addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
508e83d5887SAndrey Smetanin 		if (kvm_is_error_hva(addr))
509e83d5887SAndrey Smetanin 			return 1;
510e83d5887SAndrey Smetanin 		if (__clear_user((void __user *)addr, PAGE_SIZE))
511e83d5887SAndrey Smetanin 			return 1;
512e83d5887SAndrey Smetanin 		hv->hv_vapic = data;
513e83d5887SAndrey Smetanin 		kvm_vcpu_mark_page_dirty(vcpu, gfn);
514e83d5887SAndrey Smetanin 		if (kvm_lapic_enable_pv_eoi(vcpu,
515e83d5887SAndrey Smetanin 					    gfn_to_gpa(gfn) | KVM_MSR_ENABLED))
516e83d5887SAndrey Smetanin 			return 1;
517e83d5887SAndrey Smetanin 		break;
518e83d5887SAndrey Smetanin 	}
519e83d5887SAndrey Smetanin 	case HV_X64_MSR_EOI:
520e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
521e83d5887SAndrey Smetanin 	case HV_X64_MSR_ICR:
522e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
523e83d5887SAndrey Smetanin 	case HV_X64_MSR_TPR:
524e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
5259eec50b8SAndrey Smetanin 	case HV_X64_MSR_VP_RUNTIME:
5269eec50b8SAndrey Smetanin 		if (!host)
5279eec50b8SAndrey Smetanin 			return 1;
5289eec50b8SAndrey Smetanin 		hv->runtime_offset = data - current_task_runtime_100ns();
5299eec50b8SAndrey Smetanin 		break;
530*5c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
531*5c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
532*5c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
533*5c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
534*5c919412SAndrey Smetanin 	case HV_X64_MSR_EOM:
535*5c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
536*5c919412SAndrey Smetanin 		return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host);
537e83d5887SAndrey Smetanin 	default:
538e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
539e83d5887SAndrey Smetanin 			    msr, data);
540e83d5887SAndrey Smetanin 		return 1;
541e83d5887SAndrey Smetanin 	}
542e83d5887SAndrey Smetanin 
543e83d5887SAndrey Smetanin 	return 0;
544e83d5887SAndrey Smetanin }
545e83d5887SAndrey Smetanin 
546e83d5887SAndrey Smetanin static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
547e83d5887SAndrey Smetanin {
548e83d5887SAndrey Smetanin 	u64 data = 0;
549e83d5887SAndrey Smetanin 	struct kvm *kvm = vcpu->kvm;
550e83d5887SAndrey Smetanin 	struct kvm_hv *hv = &kvm->arch.hyperv;
551e83d5887SAndrey Smetanin 
552e83d5887SAndrey Smetanin 	switch (msr) {
553e83d5887SAndrey Smetanin 	case HV_X64_MSR_GUEST_OS_ID:
554e83d5887SAndrey Smetanin 		data = hv->hv_guest_os_id;
555e83d5887SAndrey Smetanin 		break;
556e83d5887SAndrey Smetanin 	case HV_X64_MSR_HYPERCALL:
557e83d5887SAndrey Smetanin 		data = hv->hv_hypercall;
558e83d5887SAndrey Smetanin 		break;
559e83d5887SAndrey Smetanin 	case HV_X64_MSR_TIME_REF_COUNT: {
560e83d5887SAndrey Smetanin 		data =
561e83d5887SAndrey Smetanin 		     div_u64(get_kernel_ns() + kvm->arch.kvmclock_offset, 100);
562e83d5887SAndrey Smetanin 		break;
563e83d5887SAndrey Smetanin 	}
564e83d5887SAndrey Smetanin 	case HV_X64_MSR_REFERENCE_TSC:
565e83d5887SAndrey Smetanin 		data = hv->hv_tsc_page;
566e83d5887SAndrey Smetanin 		break;
567e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
568e7d9513bSAndrey Smetanin 		return kvm_hv_msr_get_crash_data(vcpu,
569e7d9513bSAndrey Smetanin 						 msr - HV_X64_MSR_CRASH_P0,
570e7d9513bSAndrey Smetanin 						 pdata);
571e7d9513bSAndrey Smetanin 	case HV_X64_MSR_CRASH_CTL:
572e7d9513bSAndrey Smetanin 		return kvm_hv_msr_get_crash_ctl(vcpu, pdata);
573e516cebbSAndrey Smetanin 	case HV_X64_MSR_RESET:
574e516cebbSAndrey Smetanin 		data = 0;
575e516cebbSAndrey Smetanin 		break;
576e83d5887SAndrey Smetanin 	default:
577e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
578e83d5887SAndrey Smetanin 		return 1;
579e83d5887SAndrey Smetanin 	}
580e83d5887SAndrey Smetanin 
581e83d5887SAndrey Smetanin 	*pdata = data;
582e83d5887SAndrey Smetanin 	return 0;
583e83d5887SAndrey Smetanin }
584e83d5887SAndrey Smetanin 
585e83d5887SAndrey Smetanin static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
586e83d5887SAndrey Smetanin {
587e83d5887SAndrey Smetanin 	u64 data = 0;
588e83d5887SAndrey Smetanin 	struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
589e83d5887SAndrey Smetanin 
590e83d5887SAndrey Smetanin 	switch (msr) {
591e83d5887SAndrey Smetanin 	case HV_X64_MSR_VP_INDEX: {
592e83d5887SAndrey Smetanin 		int r;
593e83d5887SAndrey Smetanin 		struct kvm_vcpu *v;
594e83d5887SAndrey Smetanin 
595e83d5887SAndrey Smetanin 		kvm_for_each_vcpu(r, v, vcpu->kvm) {
596e83d5887SAndrey Smetanin 			if (v == vcpu) {
597e83d5887SAndrey Smetanin 				data = r;
598e83d5887SAndrey Smetanin 				break;
599e83d5887SAndrey Smetanin 			}
600e83d5887SAndrey Smetanin 		}
601e83d5887SAndrey Smetanin 		break;
602e83d5887SAndrey Smetanin 	}
603e83d5887SAndrey Smetanin 	case HV_X64_MSR_EOI:
604e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
605e83d5887SAndrey Smetanin 	case HV_X64_MSR_ICR:
606e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
607e83d5887SAndrey Smetanin 	case HV_X64_MSR_TPR:
608e83d5887SAndrey Smetanin 		return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
609e83d5887SAndrey Smetanin 	case HV_X64_MSR_APIC_ASSIST_PAGE:
610e83d5887SAndrey Smetanin 		data = hv->hv_vapic;
611e83d5887SAndrey Smetanin 		break;
6129eec50b8SAndrey Smetanin 	case HV_X64_MSR_VP_RUNTIME:
6139eec50b8SAndrey Smetanin 		data = current_task_runtime_100ns() + hv->runtime_offset;
6149eec50b8SAndrey Smetanin 		break;
615*5c919412SAndrey Smetanin 	case HV_X64_MSR_SCONTROL:
616*5c919412SAndrey Smetanin 	case HV_X64_MSR_SVERSION:
617*5c919412SAndrey Smetanin 	case HV_X64_MSR_SIEFP:
618*5c919412SAndrey Smetanin 	case HV_X64_MSR_SIMP:
619*5c919412SAndrey Smetanin 	case HV_X64_MSR_EOM:
620*5c919412SAndrey Smetanin 	case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
621*5c919412SAndrey Smetanin 		return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata);
622e83d5887SAndrey Smetanin 	default:
623e83d5887SAndrey Smetanin 		vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
624e83d5887SAndrey Smetanin 		return 1;
625e83d5887SAndrey Smetanin 	}
626e83d5887SAndrey Smetanin 	*pdata = data;
627e83d5887SAndrey Smetanin 	return 0;
628e83d5887SAndrey Smetanin }
629e83d5887SAndrey Smetanin 
630e7d9513bSAndrey Smetanin int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
631e83d5887SAndrey Smetanin {
632e83d5887SAndrey Smetanin 	if (kvm_hv_msr_partition_wide(msr)) {
633e83d5887SAndrey Smetanin 		int r;
634e83d5887SAndrey Smetanin 
635e83d5887SAndrey Smetanin 		mutex_lock(&vcpu->kvm->lock);
636e7d9513bSAndrey Smetanin 		r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
637e83d5887SAndrey Smetanin 		mutex_unlock(&vcpu->kvm->lock);
638e83d5887SAndrey Smetanin 		return r;
639e83d5887SAndrey Smetanin 	} else
6409eec50b8SAndrey Smetanin 		return kvm_hv_set_msr(vcpu, msr, data, host);
641e83d5887SAndrey Smetanin }
642e83d5887SAndrey Smetanin 
643e83d5887SAndrey Smetanin int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
644e83d5887SAndrey Smetanin {
645e83d5887SAndrey Smetanin 	if (kvm_hv_msr_partition_wide(msr)) {
646e83d5887SAndrey Smetanin 		int r;
647e83d5887SAndrey Smetanin 
648e83d5887SAndrey Smetanin 		mutex_lock(&vcpu->kvm->lock);
649e83d5887SAndrey Smetanin 		r = kvm_hv_get_msr_pw(vcpu, msr, pdata);
650e83d5887SAndrey Smetanin 		mutex_unlock(&vcpu->kvm->lock);
651e83d5887SAndrey Smetanin 		return r;
652e83d5887SAndrey Smetanin 	} else
653e83d5887SAndrey Smetanin 		return kvm_hv_get_msr(vcpu, msr, pdata);
654e83d5887SAndrey Smetanin }
655e83d5887SAndrey Smetanin 
656e83d5887SAndrey Smetanin bool kvm_hv_hypercall_enabled(struct kvm *kvm)
657e83d5887SAndrey Smetanin {
658e83d5887SAndrey Smetanin 	return kvm->arch.hyperv.hv_hypercall & HV_X64_MSR_HYPERCALL_ENABLE;
659e83d5887SAndrey Smetanin }
660e83d5887SAndrey Smetanin 
661e83d5887SAndrey Smetanin int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
662e83d5887SAndrey Smetanin {
663e83d5887SAndrey Smetanin 	u64 param, ingpa, outgpa, ret;
664e83d5887SAndrey Smetanin 	uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
665e83d5887SAndrey Smetanin 	bool fast, longmode;
666e83d5887SAndrey Smetanin 
667e83d5887SAndrey Smetanin 	/*
668e83d5887SAndrey Smetanin 	 * hypercall generates UD from non zero cpl and real mode
669e83d5887SAndrey Smetanin 	 * per HYPER-V spec
670e83d5887SAndrey Smetanin 	 */
671e83d5887SAndrey Smetanin 	if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
672e83d5887SAndrey Smetanin 		kvm_queue_exception(vcpu, UD_VECTOR);
673e83d5887SAndrey Smetanin 		return 0;
674e83d5887SAndrey Smetanin 	}
675e83d5887SAndrey Smetanin 
676e83d5887SAndrey Smetanin 	longmode = is_64_bit_mode(vcpu);
677e83d5887SAndrey Smetanin 
678e83d5887SAndrey Smetanin 	if (!longmode) {
679e83d5887SAndrey Smetanin 		param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
680e83d5887SAndrey Smetanin 			(kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
681e83d5887SAndrey Smetanin 		ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
682e83d5887SAndrey Smetanin 			(kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
683e83d5887SAndrey Smetanin 		outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
684e83d5887SAndrey Smetanin 			(kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
685e83d5887SAndrey Smetanin 	}
686e83d5887SAndrey Smetanin #ifdef CONFIG_X86_64
687e83d5887SAndrey Smetanin 	else {
688e83d5887SAndrey Smetanin 		param = kvm_register_read(vcpu, VCPU_REGS_RCX);
689e83d5887SAndrey Smetanin 		ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
690e83d5887SAndrey Smetanin 		outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
691e83d5887SAndrey Smetanin 	}
692e83d5887SAndrey Smetanin #endif
693e83d5887SAndrey Smetanin 
694e83d5887SAndrey Smetanin 	code = param & 0xffff;
695e83d5887SAndrey Smetanin 	fast = (param >> 16) & 0x1;
696e83d5887SAndrey Smetanin 	rep_cnt = (param >> 32) & 0xfff;
697e83d5887SAndrey Smetanin 	rep_idx = (param >> 48) & 0xfff;
698e83d5887SAndrey Smetanin 
699e83d5887SAndrey Smetanin 	trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
700e83d5887SAndrey Smetanin 
701e83d5887SAndrey Smetanin 	switch (code) {
702e83d5887SAndrey Smetanin 	case HV_X64_HV_NOTIFY_LONG_SPIN_WAIT:
703e83d5887SAndrey Smetanin 		kvm_vcpu_on_spin(vcpu);
704e83d5887SAndrey Smetanin 		break;
705e83d5887SAndrey Smetanin 	default:
706e83d5887SAndrey Smetanin 		res = HV_STATUS_INVALID_HYPERCALL_CODE;
707e83d5887SAndrey Smetanin 		break;
708e83d5887SAndrey Smetanin 	}
709e83d5887SAndrey Smetanin 
710e83d5887SAndrey Smetanin 	ret = res | (((u64)rep_done & 0xfff) << 32);
711e83d5887SAndrey Smetanin 	if (longmode) {
712e83d5887SAndrey Smetanin 		kvm_register_write(vcpu, VCPU_REGS_RAX, ret);
713e83d5887SAndrey Smetanin 	} else {
714e83d5887SAndrey Smetanin 		kvm_register_write(vcpu, VCPU_REGS_RDX, ret >> 32);
715e83d5887SAndrey Smetanin 		kvm_register_write(vcpu, VCPU_REGS_RAX, ret & 0xffffffff);
716e83d5887SAndrey Smetanin 	}
717e83d5887SAndrey Smetanin 
718e83d5887SAndrey Smetanin 	return 1;
719e83d5887SAndrey Smetanin }
720