blob: 2cffb94565f607947933d2b21008ca38609d387f [file] [log] [blame]
Andrey Smetanine83d5882015-07-03 15:01:34 +03001/*
2 * KVM Microsoft Hyper-V emulation
3 *
4 * derived from arch/x86/kvm/x86.c
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
7 * Copyright (C) 2008 Qumranet, Inc.
8 * Copyright IBM Corporation, 2008
9 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
10 * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com>
11 *
12 * Authors:
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
15 * Amit Shah <amit.shah@qumranet.com>
16 * Ben-Ami Yassour <benami@il.ibm.com>
17 * Andrey Smetanin <asmetanin@virtuozzo.com>
18 *
19 * This work is licensed under the terms of the GNU GPL, version 2. See
20 * the COPYING file in the top-level directory.
21 *
22 */
23
24#include "x86.h"
25#include "lapic.h"
Andrey Smetanin5c9194122015-11-10 15:36:34 +030026#include "ioapic.h"
Andrey Smetanine83d5882015-07-03 15:01:34 +030027#include "hyperv.h"
28
29#include <linux/kvm_host.h>
Andrey Smetanin765eaa02015-11-30 19:22:20 +030030#include <linux/highmem.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +010031#include <linux/sched/cputime.h>
Roman Kaganfaeb7832018-02-01 16:48:32 +030032#include <linux/eventfd.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +010033
Andrey Smetanin5c9194122015-11-10 15:36:34 +030034#include <asm/apicdef.h>
Andrey Smetanine83d5882015-07-03 15:01:34 +030035#include <trace/events/kvm.h>
36
37#include "trace.h"
38
Andrey Smetanin5c9194122015-11-10 15:36:34 +030039static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
40{
41 return atomic64_read(&synic->sint[sint]);
42}
43
44static inline int synic_get_sint_vector(u64 sint_value)
45{
46 if (sint_value & HV_SYNIC_SINT_MASKED)
47 return -1;
48 return sint_value & HV_SYNIC_SINT_VECTOR_MASK;
49}
50
51static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic,
52 int vector)
53{
54 int i;
55
56 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
57 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
58 return true;
59 }
60 return false;
61}
62
63static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic,
64 int vector)
65{
66 int i;
67 u64 sint_value;
68
69 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
70 sint_value = synic_read_sint(synic, i);
71 if (synic_get_sint_vector(sint_value) == vector &&
72 sint_value & HV_SYNIC_SINT_AUTO_EOI)
73 return true;
74 }
75 return false;
76}
77
Andrey Smetanin7be58a62015-12-28 18:27:23 +030078static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint,
79 u64 data, bool host)
Andrey Smetanin5c9194122015-11-10 15:36:34 +030080{
81 int vector;
82
83 vector = data & HV_SYNIC_SINT_VECTOR_MASK;
Andrey Smetanin7be58a62015-12-28 18:27:23 +030084 if (vector < 16 && !host)
Andrey Smetanin5c9194122015-11-10 15:36:34 +030085 return 1;
86 /*
87 * Guest may configure multiple SINTs to use the same vector, so
88 * we maintain a bitmap of vectors handled by synic, and a
89 * bitmap of vectors with auto-eoi behavior. The bitmaps are
90 * updated here, and atomically queried on fast paths.
91 */
92
93 atomic64_set(&synic->sint[sint], data);
94
95 if (synic_has_vector_connected(synic, vector))
96 __set_bit(vector, synic->vec_bitmap);
97 else
98 __clear_bit(vector, synic->vec_bitmap);
99
100 if (synic_has_vector_auto_eoi(synic, vector))
101 __set_bit(vector, synic->auto_eoi_bitmap);
102 else
103 __clear_bit(vector, synic->auto_eoi_bitmap);
104
105 /* Load SynIC vectors into EOI exit bitmap */
106 kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic));
107 return 0;
108}
109
Roman Kagand3457c82017-07-14 17:13:20 +0300110static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx)
111{
112 struct kvm_vcpu *vcpu = NULL;
113 int i;
114
115 if (vpidx < KVM_MAX_VCPUS)
116 vcpu = kvm_get_vcpu(kvm, vpidx);
117 if (vcpu && vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx)
118 return vcpu;
119 kvm_for_each_vcpu(i, vcpu, kvm)
120 if (vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx)
121 return vcpu;
122 return NULL;
123}
124
125static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vpidx)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300126{
127 struct kvm_vcpu *vcpu;
128 struct kvm_vcpu_hv_synic *synic;
129
Roman Kagand3457c82017-07-14 17:13:20 +0300130 vcpu = get_vcpu_by_vpidx(kvm, vpidx);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300131 if (!vcpu)
132 return NULL;
133 synic = vcpu_to_synic(vcpu);
134 return (synic->active) ? synic : NULL;
135}
136
Andrey Smetanin765eaa02015-11-30 19:22:20 +0300137static void synic_clear_sint_msg_pending(struct kvm_vcpu_hv_synic *synic,
138 u32 sint)
139{
140 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
141 struct page *page;
142 gpa_t gpa;
143 struct hv_message *msg;
144 struct hv_message_page *msg_page;
145
146 gpa = synic->msg_page & PAGE_MASK;
147 page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
148 if (is_error_page(page)) {
149 vcpu_err(vcpu, "Hyper-V SynIC can't get msg page, gpa 0x%llx\n",
150 gpa);
151 return;
152 }
153 msg_page = kmap_atomic(page);
154
155 msg = &msg_page->sint_message[sint];
156 msg->header.message_flags.msg_pending = 0;
157
158 kunmap_atomic(msg_page);
159 kvm_release_page_dirty(page);
160 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
161}
162
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300163static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
164{
165 struct kvm *kvm = vcpu->kvm;
Andrey Smetanin765eaa02015-11-30 19:22:20 +0300166 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300167 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
168 struct kvm_vcpu_hv_stimer *stimer;
169 int gsi, idx, stimers_pending;
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300170
Andrey Smetanin18659a92015-12-23 16:53:59 +0300171 trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300172
Andrey Smetanin765eaa02015-11-30 19:22:20 +0300173 if (synic->msg_page & HV_SYNIC_SIMP_ENABLE)
174 synic_clear_sint_msg_pending(synic, sint);
175
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300176 /* Try to deliver pending Hyper-V SynIC timers messages */
177 stimers_pending = 0;
178 for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) {
179 stimer = &hv_vcpu->stimer[idx];
180 if (stimer->msg_pending &&
181 (stimer->config & HV_STIMER_ENABLE) &&
182 HV_STIMER_SINT(stimer->config) == sint) {
183 set_bit(stimer->index,
184 hv_vcpu->stimer_pending_bitmap);
185 stimers_pending++;
186 }
187 }
188 if (stimers_pending)
189 kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
190
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300191 idx = srcu_read_lock(&kvm->irq_srcu);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300192 gsi = atomic_read(&synic->sint_to_gsi[sint]);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300193 if (gsi != -1)
194 kvm_notify_acked_gsi(kvm, gsi);
195 srcu_read_unlock(&kvm->irq_srcu, idx);
196}
197
Andrey Smetanindb3975712015-11-10 15:36:35 +0300198static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
199{
200 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
201 struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
202
203 hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
204 hv_vcpu->exit.u.synic.msr = msr;
205 hv_vcpu->exit.u.synic.control = synic->control;
206 hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
207 hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
208
209 kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
210}
211
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300212static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
213 u32 msr, u64 data, bool host)
214{
215 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
216 int ret;
217
218 if (!synic->active)
219 return 1;
220
Andrey Smetanin18659a92015-12-23 16:53:59 +0300221 trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host);
222
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300223 ret = 0;
224 switch (msr) {
225 case HV_X64_MSR_SCONTROL:
226 synic->control = data;
Andrey Smetanindb3975712015-11-10 15:36:35 +0300227 if (!host)
228 synic_exit(synic, msr);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300229 break;
230 case HV_X64_MSR_SVERSION:
231 if (!host) {
232 ret = 1;
233 break;
234 }
235 synic->version = data;
236 break;
237 case HV_X64_MSR_SIEFP:
Roman Kaganefc479e2017-06-22 16:51:01 +0300238 if ((data & HV_SYNIC_SIEFP_ENABLE) && !host &&
239 !synic->dont_zero_synic_pages)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300240 if (kvm_clear_guest(vcpu->kvm,
241 data & PAGE_MASK, PAGE_SIZE)) {
242 ret = 1;
243 break;
244 }
245 synic->evt_page = data;
Andrey Smetanindb3975712015-11-10 15:36:35 +0300246 if (!host)
247 synic_exit(synic, msr);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300248 break;
249 case HV_X64_MSR_SIMP:
Roman Kaganefc479e2017-06-22 16:51:01 +0300250 if ((data & HV_SYNIC_SIMP_ENABLE) && !host &&
251 !synic->dont_zero_synic_pages)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300252 if (kvm_clear_guest(vcpu->kvm,
253 data & PAGE_MASK, PAGE_SIZE)) {
254 ret = 1;
255 break;
256 }
257 synic->msg_page = data;
Andrey Smetanindb3975712015-11-10 15:36:35 +0300258 if (!host)
259 synic_exit(synic, msr);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300260 break;
261 case HV_X64_MSR_EOM: {
262 int i;
263
264 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
265 kvm_hv_notify_acked_sint(vcpu, i);
266 break;
267 }
268 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
Andrey Smetanin7be58a62015-12-28 18:27:23 +0300269 ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300270 break;
271 default:
272 ret = 1;
273 break;
274 }
275 return ret;
276}
277
278static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata)
279{
280 int ret;
281
282 if (!synic->active)
283 return 1;
284
285 ret = 0;
286 switch (msr) {
287 case HV_X64_MSR_SCONTROL:
288 *pdata = synic->control;
289 break;
290 case HV_X64_MSR_SVERSION:
291 *pdata = synic->version;
292 break;
293 case HV_X64_MSR_SIEFP:
294 *pdata = synic->evt_page;
295 break;
296 case HV_X64_MSR_SIMP:
297 *pdata = synic->msg_page;
298 break;
299 case HV_X64_MSR_EOM:
300 *pdata = 0;
301 break;
302 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
303 *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
304 break;
305 default:
306 ret = 1;
307 break;
308 }
309 return ret;
310}
311
Jiang Biaoecd8a8c2016-11-07 08:56:33 +0800312static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300313{
314 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
315 struct kvm_lapic_irq irq;
316 int ret, vector;
317
318 if (sint >= ARRAY_SIZE(synic->sint))
319 return -EINVAL;
320
321 vector = synic_get_sint_vector(synic_read_sint(synic, sint));
322 if (vector < 0)
323 return -ENOENT;
324
325 memset(&irq, 0, sizeof(irq));
Radim Krčmářf98a3ef2016-12-15 18:06:45 +0100326 irq.shorthand = APIC_DEST_SELF;
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300327 irq.dest_mode = APIC_DEST_PHYSICAL;
328 irq.delivery_mode = APIC_DM_FIXED;
329 irq.vector = vector;
330 irq.level = 1;
331
Radim Krčmářf98a3ef2016-12-15 18:06:45 +0100332 ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq, NULL);
Andrey Smetanin18659a92015-12-23 16:53:59 +0300333 trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300334 return ret;
335}
336
Roman Kagand3457c82017-07-14 17:13:20 +0300337int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vpidx, u32 sint)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300338{
339 struct kvm_vcpu_hv_synic *synic;
340
Roman Kagand3457c82017-07-14 17:13:20 +0300341 synic = synic_get(kvm, vpidx);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300342 if (!synic)
343 return -EINVAL;
344
345 return synic_set_irq(synic, sint);
346}
347
348void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
349{
350 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
351 int i;
352
Andrey Smetanin18659a92015-12-23 16:53:59 +0300353 trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300354
355 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
356 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
357 kvm_hv_notify_acked_sint(vcpu, i);
358}
359
Roman Kagand3457c82017-07-14 17:13:20 +0300360static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vpidx, u32 sint, int gsi)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300361{
362 struct kvm_vcpu_hv_synic *synic;
363
Roman Kagand3457c82017-07-14 17:13:20 +0300364 synic = synic_get(kvm, vpidx);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300365 if (!synic)
366 return -EINVAL;
367
368 if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
369 return -EINVAL;
370
371 atomic_set(&synic->sint_to_gsi[sint], gsi);
372 return 0;
373}
374
375void kvm_hv_irq_routing_update(struct kvm *kvm)
376{
377 struct kvm_irq_routing_table *irq_rt;
378 struct kvm_kernel_irq_routing_entry *e;
379 u32 gsi;
380
381 irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
382 lockdep_is_held(&kvm->irq_lock));
383
384 for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
385 hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
386 if (e->type == KVM_IRQ_ROUTING_HV_SINT)
387 kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
388 e->hv_sint.sint, gsi);
389 }
390 }
391}
392
393static void synic_init(struct kvm_vcpu_hv_synic *synic)
394{
395 int i;
396
397 memset(synic, 0, sizeof(*synic));
398 synic->version = HV_SYNIC_VERSION_1;
399 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
400 atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
401 atomic_set(&synic->sint_to_gsi[i], -1);
402 }
403}
404
Andrey Smetanin93bf4172015-11-30 19:22:19 +0300405static u64 get_time_ref_counter(struct kvm *kvm)
406{
Paolo Bonzini095cf552016-02-08 12:54:12 +0100407 struct kvm_hv *hv = &kvm->arch.hyperv;
408 struct kvm_vcpu *vcpu;
409 u64 tsc;
410
411 /*
412 * The guest has not set up the TSC page or the clock isn't
413 * stable, fall back to get_kvmclock_ns.
414 */
415 if (!hv->tsc_ref.tsc_sequence)
416 return div_u64(get_kvmclock_ns(kvm), 100);
417
418 vcpu = kvm_get_vcpu(kvm, 0);
419 tsc = kvm_read_l1_tsc(vcpu, rdtsc());
420 return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64)
421 + hv->tsc_ref.tsc_offset;
Andrey Smetanin93bf4172015-11-30 19:22:19 +0300422}
423
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300424static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300425 bool vcpu_kick)
426{
427 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
428
429 set_bit(stimer->index,
430 vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
431 kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
432 if (vcpu_kick)
433 kvm_vcpu_kick(vcpu);
434}
435
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300436static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
437{
438 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
439
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300440 trace_kvm_hv_stimer_cleanup(stimer_to_vcpu(stimer)->vcpu_id,
441 stimer->index);
442
Andrey Smetanin019b9782015-12-28 18:27:19 +0300443 hrtimer_cancel(&stimer->timer);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300444 clear_bit(stimer->index,
445 vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
446 stimer->msg_pending = false;
Andrey Smetaninf8084952015-12-28 18:27:20 +0300447 stimer->exp_time = 0;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300448}
449
450static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
451{
452 struct kvm_vcpu_hv_stimer *stimer;
453
454 stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300455 trace_kvm_hv_stimer_callback(stimer_to_vcpu(stimer)->vcpu_id,
456 stimer->index);
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300457 stimer_mark_pending(stimer, true);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300458
459 return HRTIMER_NORESTART;
460}
461
Andrey Smetaninf8084952015-12-28 18:27:20 +0300462/*
463 * stimer_start() assumptions:
464 * a) stimer->count is not equal to 0
465 * b) stimer->config has HV_STIMER_ENABLE flag
466 */
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300467static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
468{
469 u64 time_now;
470 ktime_t ktime_now;
471
472 time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm);
473 ktime_now = ktime_get();
474
475 if (stimer->config & HV_STIMER_PERIODIC) {
Andrey Smetaninf8084952015-12-28 18:27:20 +0300476 if (stimer->exp_time) {
477 if (time_now >= stimer->exp_time) {
478 u64 remainder;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300479
Andrey Smetaninf8084952015-12-28 18:27:20 +0300480 div64_u64_rem(time_now - stimer->exp_time,
481 stimer->count, &remainder);
482 stimer->exp_time =
483 time_now + (stimer->count - remainder);
484 }
485 } else
486 stimer->exp_time = time_now + stimer->count;
487
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300488 trace_kvm_hv_stimer_start_periodic(
489 stimer_to_vcpu(stimer)->vcpu_id,
490 stimer->index,
491 time_now, stimer->exp_time);
492
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300493 hrtimer_start(&stimer->timer,
Andrey Smetaninf8084952015-12-28 18:27:20 +0300494 ktime_add_ns(ktime_now,
495 100 * (stimer->exp_time - time_now)),
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300496 HRTIMER_MODE_ABS);
497 return 0;
498 }
499 stimer->exp_time = stimer->count;
500 if (time_now >= stimer->count) {
501 /*
502 * Expire timer according to Hypervisor Top-Level Functional
503 * specification v4(15.3.1):
504 * "If a one shot is enabled and the specified count is in
505 * the past, it will expire immediately."
506 */
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300507 stimer_mark_pending(stimer, false);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300508 return 0;
509 }
510
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300511 trace_kvm_hv_stimer_start_one_shot(stimer_to_vcpu(stimer)->vcpu_id,
512 stimer->index,
513 time_now, stimer->count);
514
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300515 hrtimer_start(&stimer->timer,
516 ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
517 HRTIMER_MODE_ABS);
518 return 0;
519}
520
521static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
522 bool host)
523{
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300524 trace_kvm_hv_stimer_set_config(stimer_to_vcpu(stimer)->vcpu_id,
525 stimer->index, config, host);
526
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300527 stimer_cleanup(stimer);
Andrey Smetanin23a3b202015-12-28 18:27:22 +0300528 if ((stimer->config & HV_STIMER_ENABLE) && HV_STIMER_SINT(config) == 0)
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300529 config &= ~HV_STIMER_ENABLE;
530 stimer->config = config;
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300531 stimer_mark_pending(stimer, false);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300532 return 0;
533}
534
535static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
536 bool host)
537{
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300538 trace_kvm_hv_stimer_set_count(stimer_to_vcpu(stimer)->vcpu_id,
539 stimer->index, count, host);
540
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300541 stimer_cleanup(stimer);
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300542 stimer->count = count;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300543 if (stimer->count == 0)
544 stimer->config &= ~HV_STIMER_ENABLE;
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300545 else if (stimer->config & HV_STIMER_AUTOENABLE)
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300546 stimer->config |= HV_STIMER_ENABLE;
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300547 stimer_mark_pending(stimer, false);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300548 return 0;
549}
550
551static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
552{
553 *pconfig = stimer->config;
554 return 0;
555}
556
557static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
558{
559 *pcount = stimer->count;
560 return 0;
561}
562
563static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
564 struct hv_message *src_msg)
565{
566 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
567 struct page *page;
568 gpa_t gpa;
569 struct hv_message *dst_msg;
570 int r;
571 struct hv_message_page *msg_page;
572
573 if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
574 return -ENOENT;
575
576 gpa = synic->msg_page & PAGE_MASK;
577 page = kvm_vcpu_gfn_to_page(vcpu, gpa >> PAGE_SHIFT);
578 if (is_error_page(page))
579 return -EFAULT;
580
581 msg_page = kmap_atomic(page);
582 dst_msg = &msg_page->sint_message[sint];
583 if (sync_cmpxchg(&dst_msg->header.message_type, HVMSG_NONE,
584 src_msg->header.message_type) != HVMSG_NONE) {
585 dst_msg->header.message_flags.msg_pending = 1;
586 r = -EAGAIN;
587 } else {
588 memcpy(&dst_msg->u.payload, &src_msg->u.payload,
589 src_msg->header.payload_size);
590 dst_msg->header.message_type = src_msg->header.message_type;
591 dst_msg->header.payload_size = src_msg->header.payload_size;
592 r = synic_set_irq(synic, sint);
593 if (r >= 1)
594 r = 0;
595 else if (r == 0)
596 r = -EFAULT;
597 }
598 kunmap_atomic(msg_page);
599 kvm_release_page_dirty(page);
600 kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
601 return r;
602}
603
Andrey Smetanin0cdeabb2015-12-28 18:27:21 +0300604static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300605{
606 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
607 struct hv_message *msg = &stimer->msg;
608 struct hv_timer_message_payload *payload =
609 (struct hv_timer_message_payload *)&msg->u.payload;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300610
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300611 payload->expiration_time = stimer->exp_time;
612 payload->delivery_time = get_time_ref_counter(vcpu->kvm);
Andrey Smetanin0cdeabb2015-12-28 18:27:21 +0300613 return synic_deliver_msg(vcpu_to_synic(vcpu),
614 HV_STIMER_SINT(stimer->config), msg);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300615}
616
617static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
618{
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300619 int r;
620
Andrey Smetanin0cdeabb2015-12-28 18:27:21 +0300621 stimer->msg_pending = true;
Andrey Smetaninac3e5fc2015-12-23 16:54:00 +0300622 r = stimer_send_msg(stimer);
623 trace_kvm_hv_stimer_expiration(stimer_to_vcpu(stimer)->vcpu_id,
624 stimer->index, r);
625 if (!r) {
Andrey Smetanin0cdeabb2015-12-28 18:27:21 +0300626 stimer->msg_pending = false;
627 if (!(stimer->config & HV_STIMER_PERIODIC))
628 stimer->config &= ~HV_STIMER_ENABLE;
629 }
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300630}
631
632void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
633{
634 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
635 struct kvm_vcpu_hv_stimer *stimer;
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300636 u64 time_now, exp_time;
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300637 int i;
638
639 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
640 if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
641 stimer = &hv_vcpu->stimer[i];
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300642 if (stimer->config & HV_STIMER_ENABLE) {
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300643 exp_time = stimer->exp_time;
Andrey Smetanin0cdeabb2015-12-28 18:27:21 +0300644
Andrey Smetaninf3b138c2015-12-28 18:27:24 +0300645 if (exp_time) {
646 time_now =
647 get_time_ref_counter(vcpu->kvm);
648 if (time_now >= exp_time)
649 stimer_expiration(stimer);
650 }
651
652 if ((stimer->config & HV_STIMER_ENABLE) &&
Roman Kaganf1ff89e2017-07-20 17:26:40 +0300653 stimer->count) {
654 if (!stimer->msg_pending)
655 stimer_start(stimer);
656 } else
Andrey Smetanin0cdeabb2015-12-28 18:27:21 +0300657 stimer_cleanup(stimer);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300658 }
659 }
660}
661
662void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)
663{
664 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
665 int i;
666
667 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
668 stimer_cleanup(&hv_vcpu->stimer[i]);
669}
670
671static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer)
672{
673 struct hv_message *msg = &stimer->msg;
674 struct hv_timer_message_payload *payload =
675 (struct hv_timer_message_payload *)&msg->u.payload;
676
677 memset(&msg->header, 0, sizeof(msg->header));
678 msg->header.message_type = HVMSG_TIMER_EXPIRED;
679 msg->header.payload_size = sizeof(*payload);
680
681 payload->timer_index = stimer->index;
682 payload->expiration_time = 0;
683 payload->delivery_time = 0;
684}
685
686static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index)
687{
688 memset(stimer, 0, sizeof(*stimer));
689 stimer->index = timer_index;
690 hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
691 stimer->timer.function = stimer_timer_callback;
692 stimer_prepare_msg(stimer);
693}
694
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300695void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
696{
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +0300697 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
698 int i;
699
700 synic_init(&hv_vcpu->synic);
701
702 bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
703 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
704 stimer_init(&hv_vcpu->stimer[i], i);
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300705}
706
Roman Kagand3457c82017-07-14 17:13:20 +0300707void kvm_hv_vcpu_postcreate(struct kvm_vcpu *vcpu)
708{
709 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
710
711 hv_vcpu->vp_index = kvm_vcpu_get_idx(vcpu);
712}
713
Roman Kaganefc479e2017-06-22 16:51:01 +0300714int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages)
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300715{
Roman Kaganefc479e2017-06-22 16:51:01 +0300716 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
717
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300718 /*
719 * Hyper-V SynIC auto EOI SINT's are
720 * not compatible with APICV, so deactivate APICV
721 */
722 kvm_vcpu_deactivate_apicv(vcpu);
Roman Kaganefc479e2017-06-22 16:51:01 +0300723 synic->active = true;
724 synic->dont_zero_synic_pages = dont_zero_synic_pages;
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300725 return 0;
726}
727
Andrey Smetanine83d5882015-07-03 15:01:34 +0300728static bool kvm_hv_msr_partition_wide(u32 msr)
729{
730 bool r = false;
731
732 switch (msr) {
733 case HV_X64_MSR_GUEST_OS_ID:
734 case HV_X64_MSR_HYPERCALL:
735 case HV_X64_MSR_REFERENCE_TSC:
736 case HV_X64_MSR_TIME_REF_COUNT:
Andrey Smetanine7d95132015-07-03 15:01:37 +0300737 case HV_X64_MSR_CRASH_CTL:
738 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
Andrey Smetanine516ceb2015-09-16 12:29:48 +0300739 case HV_X64_MSR_RESET:
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +0100740 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
741 case HV_X64_MSR_TSC_EMULATION_CONTROL:
742 case HV_X64_MSR_TSC_EMULATION_STATUS:
Andrey Smetanine83d5882015-07-03 15:01:34 +0300743 r = true;
744 break;
745 }
746
747 return r;
748}
749
Andrey Smetanine7d95132015-07-03 15:01:37 +0300750static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
751 u32 index, u64 *pdata)
752{
753 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
754
755 if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
756 return -EINVAL;
757
758 *pdata = hv->hv_crash_param[index];
759 return 0;
760}
761
762static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata)
763{
764 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
765
766 *pdata = hv->hv_crash_ctl;
767 return 0;
768}
769
770static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host)
771{
772 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
773
774 if (host)
775 hv->hv_crash_ctl = data & HV_X64_MSR_CRASH_CTL_NOTIFY;
776
777 if (!host && (data & HV_X64_MSR_CRASH_CTL_NOTIFY)) {
778
779 vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
780 hv->hv_crash_param[0],
781 hv->hv_crash_param[1],
782 hv->hv_crash_param[2],
783 hv->hv_crash_param[3],
784 hv->hv_crash_param[4]);
785
786 /* Send notification about crash to user space */
787 kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
788 }
789
790 return 0;
791}
792
793static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
794 u32 index, u64 data)
795{
796 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
797
798 if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
799 return -EINVAL;
800
801 hv->hv_crash_param[index] = data;
802 return 0;
803}
804
Paolo Bonzini095cf552016-02-08 12:54:12 +0100805/*
806 * The kvmclock and Hyper-V TSC page use similar formulas, and converting
807 * between them is possible:
808 *
809 * kvmclock formula:
810 * nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32)
811 * + system_time
812 *
813 * Hyper-V formula:
814 * nsec/100 = ticks * scale / 2^64 + offset
815 *
816 * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula.
817 * By dividing the kvmclock formula by 100 and equating what's left we get:
818 * ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
819 * scale / 2^64 = tsc_to_system_mul * 2^(tsc_shift-32) / 100
820 * scale = tsc_to_system_mul * 2^(32+tsc_shift) / 100
821 *
822 * Now expand the kvmclock formula and divide by 100:
823 * nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32)
824 * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32)
825 * + system_time
826 * nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
827 * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100
828 * + system_time / 100
829 *
830 * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64:
831 * nsec/100 = ticks * scale / 2^64
832 * - tsc_timestamp * scale / 2^64
833 * + system_time / 100
834 *
835 * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out:
836 * offset = system_time / 100 - tsc_timestamp * scale / 2^64
837 *
838 * These two equivalencies are implemented in this function.
839 */
840static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock,
841 HV_REFERENCE_TSC_PAGE *tsc_ref)
842{
843 u64 max_mul;
844
845 if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT))
846 return false;
847
848 /*
849 * check if scale would overflow, if so we use the time ref counter
850 * tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64
851 * tsc_to_system_mul / 100 >= 2^(32-tsc_shift)
852 * tsc_to_system_mul >= 100 * 2^(32-tsc_shift)
853 */
854 max_mul = 100ull << (32 - hv_clock->tsc_shift);
855 if (hv_clock->tsc_to_system_mul >= max_mul)
856 return false;
857
858 /*
859 * Otherwise compute the scale and offset according to the formulas
860 * derived above.
861 */
862 tsc_ref->tsc_scale =
863 mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift),
864 hv_clock->tsc_to_system_mul,
865 100);
866
867 tsc_ref->tsc_offset = hv_clock->system_time;
868 do_div(tsc_ref->tsc_offset, 100);
869 tsc_ref->tsc_offset -=
870 mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64);
871 return true;
872}
873
874void kvm_hv_setup_tsc_page(struct kvm *kvm,
875 struct pvclock_vcpu_time_info *hv_clock)
876{
877 struct kvm_hv *hv = &kvm->arch.hyperv;
878 u32 tsc_seq;
879 u64 gfn;
880
881 BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence));
882 BUILD_BUG_ON(offsetof(HV_REFERENCE_TSC_PAGE, tsc_sequence) != 0);
883
884 if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
885 return;
886
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +0100887 mutex_lock(&kvm->arch.hyperv.hv_lock);
888 if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
889 goto out_unlock;
890
Paolo Bonzini095cf552016-02-08 12:54:12 +0100891 gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
892 /*
893 * Because the TSC parameters only vary when there is a
894 * change in the master clock, do not bother with caching.
895 */
896 if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn),
897 &tsc_seq, sizeof(tsc_seq))))
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +0100898 goto out_unlock;
Paolo Bonzini095cf552016-02-08 12:54:12 +0100899
900 /*
901 * While we're computing and writing the parameters, force the
902 * guest to use the time reference count MSR.
903 */
904 hv->tsc_ref.tsc_sequence = 0;
905 if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
906 &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +0100907 goto out_unlock;
Paolo Bonzini095cf552016-02-08 12:54:12 +0100908
909 if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref))
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +0100910 goto out_unlock;
Paolo Bonzini095cf552016-02-08 12:54:12 +0100911
912 /* Ensure sequence is zero before writing the rest of the struct. */
913 smp_wmb();
914 if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +0100915 goto out_unlock;
Paolo Bonzini095cf552016-02-08 12:54:12 +0100916
917 /*
918 * Now switch to the TSC page mechanism by writing the sequence.
919 */
920 tsc_seq++;
921 if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0)
922 tsc_seq = 1;
923
924 /* Write the struct entirely before the non-zero sequence. */
925 smp_wmb();
926
927 hv->tsc_ref.tsc_sequence = tsc_seq;
928 kvm_write_guest(kvm, gfn_to_gpa(gfn),
929 &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence));
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +0100930out_unlock:
931 mutex_unlock(&kvm->arch.hyperv.hv_lock);
Paolo Bonzini095cf552016-02-08 12:54:12 +0100932}
933
Andrey Smetanine7d95132015-07-03 15:01:37 +0300934static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
935 bool host)
Andrey Smetanine83d5882015-07-03 15:01:34 +0300936{
937 struct kvm *kvm = vcpu->kvm;
938 struct kvm_hv *hv = &kvm->arch.hyperv;
939
940 switch (msr) {
941 case HV_X64_MSR_GUEST_OS_ID:
942 hv->hv_guest_os_id = data;
943 /* setting guest os id to zero disables hypercall page */
944 if (!hv->hv_guest_os_id)
945 hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
946 break;
947 case HV_X64_MSR_HYPERCALL: {
948 u64 gfn;
949 unsigned long addr;
950 u8 instructions[4];
951
952 /* if guest os id is not set hypercall should remain disabled */
953 if (!hv->hv_guest_os_id)
954 break;
955 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
956 hv->hv_hypercall = data;
957 break;
958 }
959 gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
960 addr = gfn_to_hva(kvm, gfn);
961 if (kvm_is_error_hva(addr))
962 return 1;
963 kvm_x86_ops->patch_hypercall(vcpu, instructions);
964 ((unsigned char *)instructions)[3] = 0xc3; /* ret */
965 if (__copy_to_user((void __user *)addr, instructions, 4))
966 return 1;
967 hv->hv_hypercall = data;
968 mark_page_dirty(kvm, gfn);
969 break;
970 }
Paolo Bonzini095cf552016-02-08 12:54:12 +0100971 case HV_X64_MSR_REFERENCE_TSC:
Andrey Smetanine83d5882015-07-03 15:01:34 +0300972 hv->hv_tsc_page = data;
Paolo Bonzini095cf552016-02-08 12:54:12 +0100973 if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)
974 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
Andrey Smetanine83d5882015-07-03 15:01:34 +0300975 break;
Andrey Smetanine7d95132015-07-03 15:01:37 +0300976 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
977 return kvm_hv_msr_set_crash_data(vcpu,
978 msr - HV_X64_MSR_CRASH_P0,
979 data);
980 case HV_X64_MSR_CRASH_CTL:
981 return kvm_hv_msr_set_crash_ctl(vcpu, data, host);
Andrey Smetanine516ceb2015-09-16 12:29:48 +0300982 case HV_X64_MSR_RESET:
983 if (data == 1) {
984 vcpu_debug(vcpu, "hyper-v reset requested\n");
985 kvm_make_request(KVM_REQ_HV_RESET, vcpu);
986 }
987 break;
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +0100988 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
989 hv->hv_reenlightenment_control = data;
990 break;
991 case HV_X64_MSR_TSC_EMULATION_CONTROL:
992 hv->hv_tsc_emulation_control = data;
993 break;
994 case HV_X64_MSR_TSC_EMULATION_STATUS:
995 hv->hv_tsc_emulation_status = data;
996 break;
Andrey Smetanine83d5882015-07-03 15:01:34 +0300997 default:
998 vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
999 msr, data);
1000 return 1;
1001 }
1002 return 0;
1003}
1004
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001005/* Calculate cpu time spent by current task in 100ns units */
1006static u64 current_task_runtime_100ns(void)
1007{
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001008 u64 utime, stime;
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001009
1010 task_cputime_adjusted(current, &utime, &stime);
Frederic Weisbecker5613fda2017-01-31 04:09:23 +01001011
1012 return div_u64(utime + stime, 100);
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001013}
1014
1015static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
Andrey Smetanine83d5882015-07-03 15:01:34 +03001016{
1017 struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
1018
1019 switch (msr) {
Roman Kagand3457c82017-07-14 17:13:20 +03001020 case HV_X64_MSR_VP_INDEX:
1021 if (!host)
1022 return 1;
1023 hv->vp_index = (u32)data;
1024 break;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001025 case HV_X64_MSR_APIC_ASSIST_PAGE: {
1026 u64 gfn;
1027 unsigned long addr;
1028
1029 if (!(data & HV_X64_MSR_APIC_ASSIST_PAGE_ENABLE)) {
1030 hv->hv_vapic = data;
1031 if (kvm_lapic_enable_pv_eoi(vcpu, 0))
1032 return 1;
1033 break;
1034 }
1035 gfn = data >> HV_X64_MSR_APIC_ASSIST_PAGE_ADDRESS_SHIFT;
1036 addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
1037 if (kvm_is_error_hva(addr))
1038 return 1;
1039 if (__clear_user((void __user *)addr, PAGE_SIZE))
1040 return 1;
1041 hv->hv_vapic = data;
1042 kvm_vcpu_mark_page_dirty(vcpu, gfn);
1043 if (kvm_lapic_enable_pv_eoi(vcpu,
1044 gfn_to_gpa(gfn) | KVM_MSR_ENABLED))
1045 return 1;
1046 break;
1047 }
1048 case HV_X64_MSR_EOI:
1049 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1050 case HV_X64_MSR_ICR:
1051 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1052 case HV_X64_MSR_TPR:
1053 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001054 case HV_X64_MSR_VP_RUNTIME:
1055 if (!host)
1056 return 1;
1057 hv->runtime_offset = data - current_task_runtime_100ns();
1058 break;
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001059 case HV_X64_MSR_SCONTROL:
1060 case HV_X64_MSR_SVERSION:
1061 case HV_X64_MSR_SIEFP:
1062 case HV_X64_MSR_SIMP:
1063 case HV_X64_MSR_EOM:
1064 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
1065 return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001066 case HV_X64_MSR_STIMER0_CONFIG:
1067 case HV_X64_MSR_STIMER1_CONFIG:
1068 case HV_X64_MSR_STIMER2_CONFIG:
1069 case HV_X64_MSR_STIMER3_CONFIG: {
1070 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
1071
1072 return stimer_set_config(vcpu_to_stimer(vcpu, timer_index),
1073 data, host);
1074 }
1075 case HV_X64_MSR_STIMER0_COUNT:
1076 case HV_X64_MSR_STIMER1_COUNT:
1077 case HV_X64_MSR_STIMER2_COUNT:
1078 case HV_X64_MSR_STIMER3_COUNT: {
1079 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
1080
1081 return stimer_set_count(vcpu_to_stimer(vcpu, timer_index),
1082 data, host);
1083 }
Andrey Smetanine83d5882015-07-03 15:01:34 +03001084 default:
1085 vcpu_unimpl(vcpu, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
1086 msr, data);
1087 return 1;
1088 }
1089
1090 return 0;
1091}
1092
1093static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1094{
1095 u64 data = 0;
1096 struct kvm *kvm = vcpu->kvm;
1097 struct kvm_hv *hv = &kvm->arch.hyperv;
1098
1099 switch (msr) {
1100 case HV_X64_MSR_GUEST_OS_ID:
1101 data = hv->hv_guest_os_id;
1102 break;
1103 case HV_X64_MSR_HYPERCALL:
1104 data = hv->hv_hypercall;
1105 break;
Andrey Smetanin93bf4172015-11-30 19:22:19 +03001106 case HV_X64_MSR_TIME_REF_COUNT:
1107 data = get_time_ref_counter(kvm);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001108 break;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001109 case HV_X64_MSR_REFERENCE_TSC:
1110 data = hv->hv_tsc_page;
1111 break;
Andrey Smetanine7d95132015-07-03 15:01:37 +03001112 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1113 return kvm_hv_msr_get_crash_data(vcpu,
1114 msr - HV_X64_MSR_CRASH_P0,
1115 pdata);
1116 case HV_X64_MSR_CRASH_CTL:
1117 return kvm_hv_msr_get_crash_ctl(vcpu, pdata);
Andrey Smetanine516ceb2015-09-16 12:29:48 +03001118 case HV_X64_MSR_RESET:
1119 data = 0;
1120 break;
Vitaly Kuznetsova2e164e2018-03-01 15:15:12 +01001121 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1122 data = hv->hv_reenlightenment_control;
1123 break;
1124 case HV_X64_MSR_TSC_EMULATION_CONTROL:
1125 data = hv->hv_tsc_emulation_control;
1126 break;
1127 case HV_X64_MSR_TSC_EMULATION_STATUS:
1128 data = hv->hv_tsc_emulation_status;
1129 break;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001130 default:
1131 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1132 return 1;
1133 }
1134
1135 *pdata = data;
1136 return 0;
1137}
1138
1139static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1140{
1141 u64 data = 0;
1142 struct kvm_vcpu_hv *hv = &vcpu->arch.hyperv;
1143
1144 switch (msr) {
Roman Kagand3457c82017-07-14 17:13:20 +03001145 case HV_X64_MSR_VP_INDEX:
1146 data = hv->vp_index;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001147 break;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001148 case HV_X64_MSR_EOI:
1149 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1150 case HV_X64_MSR_ICR:
1151 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1152 case HV_X64_MSR_TPR:
1153 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
1154 case HV_X64_MSR_APIC_ASSIST_PAGE:
1155 data = hv->hv_vapic;
1156 break;
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001157 case HV_X64_MSR_VP_RUNTIME:
1158 data = current_task_runtime_100ns() + hv->runtime_offset;
1159 break;
Andrey Smetanin5c9194122015-11-10 15:36:34 +03001160 case HV_X64_MSR_SCONTROL:
1161 case HV_X64_MSR_SVERSION:
1162 case HV_X64_MSR_SIEFP:
1163 case HV_X64_MSR_SIMP:
1164 case HV_X64_MSR_EOM:
1165 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
1166 return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata);
Andrey Smetanin1f4b34f2015-11-30 19:22:21 +03001167 case HV_X64_MSR_STIMER0_CONFIG:
1168 case HV_X64_MSR_STIMER1_CONFIG:
1169 case HV_X64_MSR_STIMER2_CONFIG:
1170 case HV_X64_MSR_STIMER3_CONFIG: {
1171 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
1172
1173 return stimer_get_config(vcpu_to_stimer(vcpu, timer_index),
1174 pdata);
1175 }
1176 case HV_X64_MSR_STIMER0_COUNT:
1177 case HV_X64_MSR_STIMER1_COUNT:
1178 case HV_X64_MSR_STIMER2_COUNT:
1179 case HV_X64_MSR_STIMER3_COUNT: {
1180 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
1181
1182 return stimer_get_count(vcpu_to_stimer(vcpu, timer_index),
1183 pdata);
1184 }
Ladi Prosek72c139b2017-07-26 13:32:59 +02001185 case HV_X64_MSR_TSC_FREQUENCY:
1186 data = (u64)vcpu->arch.virtual_tsc_khz * 1000;
1187 break;
1188 case HV_X64_MSR_APIC_FREQUENCY:
1189 data = APIC_BUS_FREQUENCY;
1190 break;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001191 default:
1192 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1193 return 1;
1194 }
1195 *pdata = data;
1196 return 0;
1197}
1198
Andrey Smetanine7d95132015-07-03 15:01:37 +03001199int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
Andrey Smetanine83d5882015-07-03 15:01:34 +03001200{
1201 if (kvm_hv_msr_partition_wide(msr)) {
1202 int r;
1203
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +01001204 mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
Andrey Smetanine7d95132015-07-03 15:01:37 +03001205 r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +01001206 mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001207 return r;
1208 } else
Andrey Smetanin9eec50b2015-09-16 12:29:50 +03001209 return kvm_hv_set_msr(vcpu, msr, data, host);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001210}
1211
1212int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
1213{
1214 if (kvm_hv_msr_partition_wide(msr)) {
1215 int r;
1216
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +01001217 mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001218 r = kvm_hv_get_msr_pw(vcpu, msr, pdata);
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +01001219 mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001220 return r;
1221 } else
1222 return kvm_hv_get_msr(vcpu, msr, pdata);
1223}
1224
1225bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1226{
Paolo Bonzini3f5ad8b2016-12-12 10:12:53 +01001227 return READ_ONCE(kvm->arch.hyperv.hv_hypercall) & HV_X64_MSR_HYPERCALL_ENABLE;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001228}
1229
Andrey Smetanin83326e42016-02-11 16:45:01 +03001230static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
1231{
1232 bool longmode;
1233
1234 longmode = is_64_bit_mode(vcpu);
1235 if (longmode)
1236 kvm_register_write(vcpu, VCPU_REGS_RAX, result);
1237 else {
1238 kvm_register_write(vcpu, VCPU_REGS_RDX, result >> 32);
1239 kvm_register_write(vcpu, VCPU_REGS_RAX, result & 0xffffffff);
1240 }
1241}
1242
1243static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
1244{
1245 struct kvm_run *run = vcpu->run;
1246
1247 kvm_hv_hypercall_set_result(vcpu, run->hyperv.u.hcall.result);
1248 return 1;
1249}
1250
Roman Kaganfaeb7832018-02-01 16:48:32 +03001251static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, bool fast, u64 param)
1252{
1253 struct eventfd_ctx *eventfd;
1254
1255 if (unlikely(!fast)) {
1256 int ret;
1257 gpa_t gpa = param;
1258
1259 if ((gpa & (__alignof__(param) - 1)) ||
1260 offset_in_page(gpa) + sizeof(param) > PAGE_SIZE)
1261 return HV_STATUS_INVALID_ALIGNMENT;
1262
1263 ret = kvm_vcpu_read_guest(vcpu, gpa, &param, sizeof(param));
1264 if (ret < 0)
1265 return HV_STATUS_INVALID_ALIGNMENT;
1266 }
1267
1268 /*
1269 * Per spec, bits 32-47 contain the extra "flag number". However, we
1270 * have no use for it, and in all known usecases it is zero, so just
1271 * report lookup failure if it isn't.
1272 */
1273 if (param & 0xffff00000000ULL)
1274 return HV_STATUS_INVALID_PORT_ID;
1275 /* remaining bits are reserved-zero */
1276 if (param & ~KVM_HYPERV_CONN_ID_MASK)
1277 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1278
1279 /* conn_to_evt is protected by vcpu->kvm->srcu */
1280 eventfd = idr_find(&vcpu->kvm->arch.hyperv.conn_to_evt, param);
1281 if (!eventfd)
1282 return HV_STATUS_INVALID_PORT_ID;
1283
1284 eventfd_signal(eventfd, 1);
1285 return HV_STATUS_SUCCESS;
1286}
1287
Andrey Smetanine83d5882015-07-03 15:01:34 +03001288int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
1289{
1290 u64 param, ingpa, outgpa, ret;
1291 uint16_t code, rep_idx, rep_cnt, res = HV_STATUS_SUCCESS, rep_done = 0;
1292 bool fast, longmode;
1293
1294 /*
1295 * hypercall generates UD from non zero cpl and real mode
1296 * per HYPER-V spec
1297 */
1298 if (kvm_x86_ops->get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
1299 kvm_queue_exception(vcpu, UD_VECTOR);
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03001300 return 1;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001301 }
1302
1303 longmode = is_64_bit_mode(vcpu);
1304
1305 if (!longmode) {
1306 param = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDX) << 32) |
1307 (kvm_register_read(vcpu, VCPU_REGS_RAX) & 0xffffffff);
1308 ingpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RBX) << 32) |
1309 (kvm_register_read(vcpu, VCPU_REGS_RCX) & 0xffffffff);
1310 outgpa = ((u64)kvm_register_read(vcpu, VCPU_REGS_RDI) << 32) |
1311 (kvm_register_read(vcpu, VCPU_REGS_RSI) & 0xffffffff);
1312 }
1313#ifdef CONFIG_X86_64
1314 else {
1315 param = kvm_register_read(vcpu, VCPU_REGS_RCX);
1316 ingpa = kvm_register_read(vcpu, VCPU_REGS_RDX);
1317 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
1318 }
1319#endif
1320
1321 code = param & 0xffff;
1322 fast = (param >> 16) & 0x1;
1323 rep_cnt = (param >> 32) & 0xfff;
1324 rep_idx = (param >> 48) & 0xfff;
1325
1326 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
1327
Andrey Smetaninb2fdc252016-02-11 16:45:00 +03001328 /* Hypercall continuation is not supported yet */
1329 if (rep_cnt || rep_idx) {
1330 res = HV_STATUS_INVALID_HYPERCALL_CODE;
1331 goto set_result;
1332 }
1333
Andrey Smetanine83d5882015-07-03 15:01:34 +03001334 switch (code) {
Andrey Smetanin8ed6d762016-02-11 16:44:57 +03001335 case HVCALL_NOTIFY_LONG_SPIN_WAIT:
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08001336 kvm_vcpu_on_spin(vcpu, true);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001337 break;
Andrey Smetanin83326e42016-02-11 16:45:01 +03001338 case HVCALL_SIGNAL_EVENT:
Roman Kaganfaeb7832018-02-01 16:48:32 +03001339 res = kvm_hvcall_signal_event(vcpu, fast, ingpa);
1340 if (res != HV_STATUS_INVALID_PORT_ID)
1341 break;
1342 /* maybe userspace knows this conn_id: fall through */
1343 case HVCALL_POST_MESSAGE:
Paolo Bonzinia2b5c3c2016-03-29 11:23:25 +02001344 /* don't bother userspace if it has no way to handle it */
1345 if (!vcpu_to_synic(vcpu)->active) {
1346 res = HV_STATUS_INVALID_HYPERCALL_CODE;
1347 break;
1348 }
Andrey Smetanin83326e42016-02-11 16:45:01 +03001349 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
1350 vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
1351 vcpu->run->hyperv.u.hcall.input = param;
1352 vcpu->run->hyperv.u.hcall.params[0] = ingpa;
1353 vcpu->run->hyperv.u.hcall.params[1] = outgpa;
1354 vcpu->arch.complete_userspace_io =
1355 kvm_hv_hypercall_complete_userspace;
1356 return 0;
Andrey Smetanine83d5882015-07-03 15:01:34 +03001357 default:
1358 res = HV_STATUS_INVALID_HYPERCALL_CODE;
1359 break;
1360 }
1361
Andrey Smetaninb2fdc252016-02-11 16:45:00 +03001362set_result:
Andrey Smetanine83d5882015-07-03 15:01:34 +03001363 ret = res | (((u64)rep_done & 0xfff) << 32);
Andrey Smetanin83326e42016-02-11 16:45:01 +03001364 kvm_hv_hypercall_set_result(vcpu, ret);
Andrey Smetanine83d5882015-07-03 15:01:34 +03001365 return 1;
1366}
Roman Kagancbc02362018-02-01 16:48:31 +03001367
1368void kvm_hv_init_vm(struct kvm *kvm)
1369{
1370 mutex_init(&kvm->arch.hyperv.hv_lock);
Roman Kaganfaeb7832018-02-01 16:48:32 +03001371 idr_init(&kvm->arch.hyperv.conn_to_evt);
Roman Kagancbc02362018-02-01 16:48:31 +03001372}
1373
1374void kvm_hv_destroy_vm(struct kvm *kvm)
1375{
Roman Kaganfaeb7832018-02-01 16:48:32 +03001376 struct eventfd_ctx *eventfd;
1377 int i;
1378
1379 idr_for_each_entry(&kvm->arch.hyperv.conn_to_evt, eventfd, i)
1380 eventfd_ctx_put(eventfd);
1381 idr_destroy(&kvm->arch.hyperv.conn_to_evt);
1382}
1383
1384static int kvm_hv_eventfd_assign(struct kvm *kvm, u32 conn_id, int fd)
1385{
1386 struct kvm_hv *hv = &kvm->arch.hyperv;
1387 struct eventfd_ctx *eventfd;
1388 int ret;
1389
1390 eventfd = eventfd_ctx_fdget(fd);
1391 if (IS_ERR(eventfd))
1392 return PTR_ERR(eventfd);
1393
1394 mutex_lock(&hv->hv_lock);
1395 ret = idr_alloc(&hv->conn_to_evt, eventfd, conn_id, conn_id + 1,
1396 GFP_KERNEL);
1397 mutex_unlock(&hv->hv_lock);
1398
1399 if (ret >= 0)
1400 return 0;
1401
1402 if (ret == -ENOSPC)
1403 ret = -EEXIST;
1404 eventfd_ctx_put(eventfd);
1405 return ret;
1406}
1407
1408static int kvm_hv_eventfd_deassign(struct kvm *kvm, u32 conn_id)
1409{
1410 struct kvm_hv *hv = &kvm->arch.hyperv;
1411 struct eventfd_ctx *eventfd;
1412
1413 mutex_lock(&hv->hv_lock);
1414 eventfd = idr_remove(&hv->conn_to_evt, conn_id);
1415 mutex_unlock(&hv->hv_lock);
1416
1417 if (!eventfd)
1418 return -ENOENT;
1419
1420 synchronize_srcu(&kvm->srcu);
1421 eventfd_ctx_put(eventfd);
1422 return 0;
1423}
1424
1425int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args)
1426{
1427 if ((args->flags & ~KVM_HYPERV_EVENTFD_DEASSIGN) ||
1428 (args->conn_id & ~KVM_HYPERV_CONN_ID_MASK))
1429 return -EINVAL;
1430
1431 if (args->flags == KVM_HYPERV_EVENTFD_DEASSIGN)
1432 return kvm_hv_eventfd_deassign(kvm, args->conn_id);
1433 return kvm_hv_eventfd_assign(kvm, args->conn_id, args->fd);
Roman Kagancbc02362018-02-01 16:48:31 +03001434}