1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * KVM Microsoft Hyper-V emulation
4 *
5 * derived from arch/x86/kvm/x86.c
6 *
7 * Copyright (C) 2006 Qumranet, Inc.
8 * Copyright (C) 2008 Qumranet, Inc.
9 * Copyright IBM Corporation, 2008
10 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
11 * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com>
12 *
13 * Authors:
14 * Avi Kivity <avi@qumranet.com>
15 * Yaniv Kamay <yaniv@qumranet.com>
16 * Amit Shah <amit.shah@qumranet.com>
17 * Ben-Ami Yassour <benami@il.ibm.com>
18 * Andrey Smetanin <asmetanin@virtuozzo.com>
19 */
20
21 #include "x86.h"
22 #include "lapic.h"
23 #include "ioapic.h"
24 #include "cpuid.h"
25 #include "hyperv.h"
26
27 #include <linux/cpu.h>
28 #include <linux/kvm_host.h>
29 #include <linux/highmem.h>
30 #include <linux/sched/cputime.h>
31 #include <linux/eventfd.h>
32
33 #include <asm/apicdef.h>
34 #include <trace/events/kvm.h>
35
36 #include "trace.h"
37 #include "irq.h"
38
39 #define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, 64)
40
41 static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
42 bool vcpu_kick);
43
synic_read_sint(struct kvm_vcpu_hv_synic * synic,int sint)44 static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
45 {
46 return atomic64_read(&synic->sint[sint]);
47 }
48
synic_get_sint_vector(u64 sint_value)49 static inline int synic_get_sint_vector(u64 sint_value)
50 {
51 if (sint_value & HV_SYNIC_SINT_MASKED)
52 return -1;
53 return sint_value & HV_SYNIC_SINT_VECTOR_MASK;
54 }
55
synic_has_vector_connected(struct kvm_vcpu_hv_synic * synic,int vector)56 static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic,
57 int vector)
58 {
59 int i;
60
61 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
62 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
63 return true;
64 }
65 return false;
66 }
67
synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic * synic,int vector)68 static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic,
69 int vector)
70 {
71 int i;
72 u64 sint_value;
73
74 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
75 sint_value = synic_read_sint(synic, i);
76 if (synic_get_sint_vector(sint_value) == vector &&
77 sint_value & HV_SYNIC_SINT_AUTO_EOI)
78 return true;
79 }
80 return false;
81 }
82
synic_update_vector(struct kvm_vcpu_hv_synic * synic,int vector)83 static void synic_update_vector(struct kvm_vcpu_hv_synic *synic,
84 int vector)
85 {
86 if (vector < HV_SYNIC_FIRST_VALID_VECTOR)
87 return;
88
89 if (synic_has_vector_connected(synic, vector))
90 __set_bit(vector, synic->vec_bitmap);
91 else
92 __clear_bit(vector, synic->vec_bitmap);
93
94 if (synic_has_vector_auto_eoi(synic, vector))
95 __set_bit(vector, synic->auto_eoi_bitmap);
96 else
97 __clear_bit(vector, synic->auto_eoi_bitmap);
98 }
99
synic_set_sint(struct kvm_vcpu_hv_synic * synic,int sint,u64 data,bool host)100 static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint,
101 u64 data, bool host)
102 {
103 int vector, old_vector;
104 bool masked;
105
106 vector = data & HV_SYNIC_SINT_VECTOR_MASK;
107 masked = data & HV_SYNIC_SINT_MASKED;
108
109 /*
110 * Valid vectors are 16-255, however, nested Hyper-V attempts to write
111 * default '0x10000' value on boot and this should not #GP. We need to
112 * allow zero-initing the register from host as well.
113 */
114 if (vector < HV_SYNIC_FIRST_VALID_VECTOR && !host && !masked)
115 return 1;
116 /*
117 * Guest may configure multiple SINTs to use the same vector, so
118 * we maintain a bitmap of vectors handled by synic, and a
119 * bitmap of vectors with auto-eoi behavior. The bitmaps are
120 * updated here, and atomically queried on fast paths.
121 */
122 old_vector = synic_read_sint(synic, sint) & HV_SYNIC_SINT_VECTOR_MASK;
123
124 atomic64_set(&synic->sint[sint], data);
125
126 synic_update_vector(synic, old_vector);
127
128 synic_update_vector(synic, vector);
129
130 /* Load SynIC vectors into EOI exit bitmap */
131 kvm_make_request(KVM_REQ_SCAN_IOAPIC, synic_to_vcpu(synic));
132 return 0;
133 }
134
get_vcpu_by_vpidx(struct kvm * kvm,u32 vpidx)135 static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx)
136 {
137 struct kvm_vcpu *vcpu = NULL;
138 int i;
139
140 if (vpidx >= KVM_MAX_VCPUS)
141 return NULL;
142
143 vcpu = kvm_get_vcpu(kvm, vpidx);
144 if (vcpu && vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx)
145 return vcpu;
146 kvm_for_each_vcpu(i, vcpu, kvm)
147 if (vcpu_to_hv_vcpu(vcpu)->vp_index == vpidx)
148 return vcpu;
149 return NULL;
150 }
151
synic_get(struct kvm * kvm,u32 vpidx)152 static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vpidx)
153 {
154 struct kvm_vcpu *vcpu;
155 struct kvm_vcpu_hv_synic *synic;
156
157 vcpu = get_vcpu_by_vpidx(kvm, vpidx);
158 if (!vcpu)
159 return NULL;
160 synic = vcpu_to_synic(vcpu);
161 return (synic->active) ? synic : NULL;
162 }
163
kvm_hv_notify_acked_sint(struct kvm_vcpu * vcpu,u32 sint)164 static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
165 {
166 struct kvm *kvm = vcpu->kvm;
167 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
168 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
169 struct kvm_vcpu_hv_stimer *stimer;
170 int gsi, idx;
171
172 trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint);
173
174 /* Try to deliver pending Hyper-V SynIC timers messages */
175 for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) {
176 stimer = &hv_vcpu->stimer[idx];
177 if (stimer->msg_pending && stimer->config.enable &&
178 !stimer->config.direct_mode &&
179 stimer->config.sintx == sint)
180 stimer_mark_pending(stimer, false);
181 }
182
183 idx = srcu_read_lock(&kvm->irq_srcu);
184 gsi = atomic_read(&synic->sint_to_gsi[sint]);
185 if (gsi != -1)
186 kvm_notify_acked_gsi(kvm, gsi);
187 srcu_read_unlock(&kvm->irq_srcu, idx);
188 }
189
synic_exit(struct kvm_vcpu_hv_synic * synic,u32 msr)190 static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
191 {
192 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
193 struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
194
195 hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
196 hv_vcpu->exit.u.synic.msr = msr;
197 hv_vcpu->exit.u.synic.control = synic->control;
198 hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
199 hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
200
201 kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
202 }
203
synic_set_msr(struct kvm_vcpu_hv_synic * synic,u32 msr,u64 data,bool host)204 static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
205 u32 msr, u64 data, bool host)
206 {
207 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
208 int ret;
209
210 if (!synic->active && (!host || data))
211 return 1;
212
213 trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host);
214
215 ret = 0;
216 switch (msr) {
217 case HV_X64_MSR_SCONTROL:
218 synic->control = data;
219 if (!host)
220 synic_exit(synic, msr);
221 break;
222 case HV_X64_MSR_SVERSION:
223 if (!host) {
224 ret = 1;
225 break;
226 }
227 synic->version = data;
228 break;
229 case HV_X64_MSR_SIEFP:
230 if ((data & HV_SYNIC_SIEFP_ENABLE) && !host &&
231 !synic->dont_zero_synic_pages)
232 if (kvm_clear_guest(vcpu->kvm,
233 data & PAGE_MASK, PAGE_SIZE)) {
234 ret = 1;
235 break;
236 }
237 synic->evt_page = data;
238 if (!host)
239 synic_exit(synic, msr);
240 break;
241 case HV_X64_MSR_SIMP:
242 if ((data & HV_SYNIC_SIMP_ENABLE) && !host &&
243 !synic->dont_zero_synic_pages)
244 if (kvm_clear_guest(vcpu->kvm,
245 data & PAGE_MASK, PAGE_SIZE)) {
246 ret = 1;
247 break;
248 }
249 synic->msg_page = data;
250 if (!host)
251 synic_exit(synic, msr);
252 break;
253 case HV_X64_MSR_EOM: {
254 int i;
255
256 if (!synic->active)
257 break;
258
259 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
260 kvm_hv_notify_acked_sint(vcpu, i);
261 break;
262 }
263 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
264 ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host);
265 break;
266 default:
267 ret = 1;
268 break;
269 }
270 return ret;
271 }
272
kvm_hv_is_syndbg_enabled(struct kvm_vcpu * vcpu)273 static bool kvm_hv_is_syndbg_enabled(struct kvm_vcpu *vcpu)
274 {
275 struct kvm_cpuid_entry2 *entry;
276
277 entry = kvm_find_cpuid_entry(vcpu,
278 HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES,
279 0);
280 if (!entry)
281 return false;
282
283 return entry->eax & HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING;
284 }
285
kvm_hv_syndbg_complete_userspace(struct kvm_vcpu * vcpu)286 static int kvm_hv_syndbg_complete_userspace(struct kvm_vcpu *vcpu)
287 {
288 struct kvm *kvm = vcpu->kvm;
289 struct kvm_hv *hv = &kvm->arch.hyperv;
290
291 if (vcpu->run->hyperv.u.syndbg.msr == HV_X64_MSR_SYNDBG_CONTROL)
292 hv->hv_syndbg.control.status =
293 vcpu->run->hyperv.u.syndbg.status;
294 return 1;
295 }
296
syndbg_exit(struct kvm_vcpu * vcpu,u32 msr)297 static void syndbg_exit(struct kvm_vcpu *vcpu, u32 msr)
298 {
299 struct kvm_hv_syndbg *syndbg = vcpu_to_hv_syndbg(vcpu);
300 struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
301
302 hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNDBG;
303 hv_vcpu->exit.u.syndbg.msr = msr;
304 hv_vcpu->exit.u.syndbg.control = syndbg->control.control;
305 hv_vcpu->exit.u.syndbg.send_page = syndbg->control.send_page;
306 hv_vcpu->exit.u.syndbg.recv_page = syndbg->control.recv_page;
307 hv_vcpu->exit.u.syndbg.pending_page = syndbg->control.pending_page;
308 vcpu->arch.complete_userspace_io =
309 kvm_hv_syndbg_complete_userspace;
310
311 kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
312 }
313
syndbg_set_msr(struct kvm_vcpu * vcpu,u32 msr,u64 data,bool host)314 static int syndbg_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
315 {
316 struct kvm_hv_syndbg *syndbg = vcpu_to_hv_syndbg(vcpu);
317
318 if (!kvm_hv_is_syndbg_enabled(vcpu) && !host)
319 return 1;
320
321 trace_kvm_hv_syndbg_set_msr(vcpu->vcpu_id,
322 vcpu_to_hv_vcpu(vcpu)->vp_index, msr, data);
323 switch (msr) {
324 case HV_X64_MSR_SYNDBG_CONTROL:
325 syndbg->control.control = data;
326 if (!host)
327 syndbg_exit(vcpu, msr);
328 break;
329 case HV_X64_MSR_SYNDBG_STATUS:
330 syndbg->control.status = data;
331 break;
332 case HV_X64_MSR_SYNDBG_SEND_BUFFER:
333 syndbg->control.send_page = data;
334 break;
335 case HV_X64_MSR_SYNDBG_RECV_BUFFER:
336 syndbg->control.recv_page = data;
337 break;
338 case HV_X64_MSR_SYNDBG_PENDING_BUFFER:
339 syndbg->control.pending_page = data;
340 if (!host)
341 syndbg_exit(vcpu, msr);
342 break;
343 case HV_X64_MSR_SYNDBG_OPTIONS:
344 syndbg->options = data;
345 break;
346 default:
347 break;
348 }
349
350 return 0;
351 }
352
syndbg_get_msr(struct kvm_vcpu * vcpu,u32 msr,u64 * pdata,bool host)353 static int syndbg_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
354 {
355 struct kvm_hv_syndbg *syndbg = vcpu_to_hv_syndbg(vcpu);
356
357 if (!kvm_hv_is_syndbg_enabled(vcpu) && !host)
358 return 1;
359
360 switch (msr) {
361 case HV_X64_MSR_SYNDBG_CONTROL:
362 *pdata = syndbg->control.control;
363 break;
364 case HV_X64_MSR_SYNDBG_STATUS:
365 *pdata = syndbg->control.status;
366 break;
367 case HV_X64_MSR_SYNDBG_SEND_BUFFER:
368 *pdata = syndbg->control.send_page;
369 break;
370 case HV_X64_MSR_SYNDBG_RECV_BUFFER:
371 *pdata = syndbg->control.recv_page;
372 break;
373 case HV_X64_MSR_SYNDBG_PENDING_BUFFER:
374 *pdata = syndbg->control.pending_page;
375 break;
376 case HV_X64_MSR_SYNDBG_OPTIONS:
377 *pdata = syndbg->options;
378 break;
379 default:
380 break;
381 }
382
383 trace_kvm_hv_syndbg_get_msr(vcpu->vcpu_id,
384 vcpu_to_hv_vcpu(vcpu)->vp_index, msr,
385 *pdata);
386
387 return 0;
388 }
389
synic_get_msr(struct kvm_vcpu_hv_synic * synic,u32 msr,u64 * pdata,bool host)390 static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata,
391 bool host)
392 {
393 int ret;
394
395 if (!synic->active && !host)
396 return 1;
397
398 ret = 0;
399 switch (msr) {
400 case HV_X64_MSR_SCONTROL:
401 *pdata = synic->control;
402 break;
403 case HV_X64_MSR_SVERSION:
404 *pdata = synic->version;
405 break;
406 case HV_X64_MSR_SIEFP:
407 *pdata = synic->evt_page;
408 break;
409 case HV_X64_MSR_SIMP:
410 *pdata = synic->msg_page;
411 break;
412 case HV_X64_MSR_EOM:
413 *pdata = 0;
414 break;
415 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
416 *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
417 break;
418 default:
419 ret = 1;
420 break;
421 }
422 return ret;
423 }
424
synic_set_irq(struct kvm_vcpu_hv_synic * synic,u32 sint)425 static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
426 {
427 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
428 struct kvm_lapic_irq irq;
429 int ret, vector;
430
431 if (sint >= ARRAY_SIZE(synic->sint))
432 return -EINVAL;
433
434 vector = synic_get_sint_vector(synic_read_sint(synic, sint));
435 if (vector < 0)
436 return -ENOENT;
437
438 memset(&irq, 0, sizeof(irq));
439 irq.shorthand = APIC_DEST_SELF;
440 irq.dest_mode = APIC_DEST_PHYSICAL;
441 irq.delivery_mode = APIC_DM_FIXED;
442 irq.vector = vector;
443 irq.level = 1;
444
445 ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq, NULL);
446 trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret);
447 return ret;
448 }
449
kvm_hv_synic_set_irq(struct kvm * kvm,u32 vpidx,u32 sint)450 int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vpidx, u32 sint)
451 {
452 struct kvm_vcpu_hv_synic *synic;
453
454 synic = synic_get(kvm, vpidx);
455 if (!synic)
456 return -EINVAL;
457
458 return synic_set_irq(synic, sint);
459 }
460
kvm_hv_synic_send_eoi(struct kvm_vcpu * vcpu,int vector)461 void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
462 {
463 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
464 int i;
465
466 trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector);
467
468 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
469 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
470 kvm_hv_notify_acked_sint(vcpu, i);
471 }
472
kvm_hv_set_sint_gsi(struct kvm * kvm,u32 vpidx,u32 sint,int gsi)473 static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vpidx, u32 sint, int gsi)
474 {
475 struct kvm_vcpu_hv_synic *synic;
476
477 synic = synic_get(kvm, vpidx);
478 if (!synic)
479 return -EINVAL;
480
481 if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
482 return -EINVAL;
483
484 atomic_set(&synic->sint_to_gsi[sint], gsi);
485 return 0;
486 }
487
kvm_hv_irq_routing_update(struct kvm * kvm)488 void kvm_hv_irq_routing_update(struct kvm *kvm)
489 {
490 struct kvm_irq_routing_table *irq_rt;
491 struct kvm_kernel_irq_routing_entry *e;
492 u32 gsi;
493
494 irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
495 lockdep_is_held(&kvm->irq_lock));
496
497 for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
498 hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
499 if (e->type == KVM_IRQ_ROUTING_HV_SINT)
500 kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
501 e->hv_sint.sint, gsi);
502 }
503 }
504 }
505
synic_init(struct kvm_vcpu_hv_synic * synic)506 static void synic_init(struct kvm_vcpu_hv_synic *synic)
507 {
508 int i;
509
510 memset(synic, 0, sizeof(*synic));
511 synic->version = HV_SYNIC_VERSION_1;
512 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
513 atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
514 atomic_set(&synic->sint_to_gsi[i], -1);
515 }
516 }
517
get_time_ref_counter(struct kvm * kvm)518 static u64 get_time_ref_counter(struct kvm *kvm)
519 {
520 struct kvm_hv *hv = &kvm->arch.hyperv;
521 struct kvm_vcpu *vcpu;
522 u64 tsc;
523
524 /*
525 * The guest has not set up the TSC page or the clock isn't
526 * stable, fall back to get_kvmclock_ns.
527 */
528 if (!hv->tsc_ref.tsc_sequence)
529 return div_u64(get_kvmclock_ns(kvm), 100);
530
531 vcpu = kvm_get_vcpu(kvm, 0);
532 tsc = kvm_read_l1_tsc(vcpu, rdtsc());
533 return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64)
534 + hv->tsc_ref.tsc_offset;
535 }
536
stimer_mark_pending(struct kvm_vcpu_hv_stimer * stimer,bool vcpu_kick)537 static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
538 bool vcpu_kick)
539 {
540 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
541
542 set_bit(stimer->index,
543 vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
544 kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
545 if (vcpu_kick)
546 kvm_vcpu_kick(vcpu);
547 }
548
stimer_cleanup(struct kvm_vcpu_hv_stimer * stimer)549 static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
550 {
551 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
552
553 trace_kvm_hv_stimer_cleanup(stimer_to_vcpu(stimer)->vcpu_id,
554 stimer->index);
555
556 hrtimer_cancel(&stimer->timer);
557 clear_bit(stimer->index,
558 vcpu_to_hv_vcpu(vcpu)->stimer_pending_bitmap);
559 stimer->msg_pending = false;
560 stimer->exp_time = 0;
561 }
562
stimer_timer_callback(struct hrtimer * timer)563 static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
564 {
565 struct kvm_vcpu_hv_stimer *stimer;
566
567 stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
568 trace_kvm_hv_stimer_callback(stimer_to_vcpu(stimer)->vcpu_id,
569 stimer->index);
570 stimer_mark_pending(stimer, true);
571
572 return HRTIMER_NORESTART;
573 }
574
575 /*
576 * stimer_start() assumptions:
577 * a) stimer->count is not equal to 0
578 * b) stimer->config has HV_STIMER_ENABLE flag
579 */
stimer_start(struct kvm_vcpu_hv_stimer * stimer)580 static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
581 {
582 u64 time_now;
583 ktime_t ktime_now;
584
585 time_now = get_time_ref_counter(stimer_to_vcpu(stimer)->kvm);
586 ktime_now = ktime_get();
587
588 if (stimer->config.periodic) {
589 if (stimer->exp_time) {
590 if (time_now >= stimer->exp_time) {
591 u64 remainder;
592
593 div64_u64_rem(time_now - stimer->exp_time,
594 stimer->count, &remainder);
595 stimer->exp_time =
596 time_now + (stimer->count - remainder);
597 }
598 } else
599 stimer->exp_time = time_now + stimer->count;
600
601 trace_kvm_hv_stimer_start_periodic(
602 stimer_to_vcpu(stimer)->vcpu_id,
603 stimer->index,
604 time_now, stimer->exp_time);
605
606 hrtimer_start(&stimer->timer,
607 ktime_add_ns(ktime_now,
608 100 * (stimer->exp_time - time_now)),
609 HRTIMER_MODE_ABS);
610 return 0;
611 }
612 stimer->exp_time = stimer->count;
613 if (time_now >= stimer->count) {
614 /*
615 * Expire timer according to Hypervisor Top-Level Functional
616 * specification v4(15.3.1):
617 * "If a one shot is enabled and the specified count is in
618 * the past, it will expire immediately."
619 */
620 stimer_mark_pending(stimer, false);
621 return 0;
622 }
623
624 trace_kvm_hv_stimer_start_one_shot(stimer_to_vcpu(stimer)->vcpu_id,
625 stimer->index,
626 time_now, stimer->count);
627
628 hrtimer_start(&stimer->timer,
629 ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
630 HRTIMER_MODE_ABS);
631 return 0;
632 }
633
stimer_set_config(struct kvm_vcpu_hv_stimer * stimer,u64 config,bool host)634 static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
635 bool host)
636 {
637 union hv_stimer_config new_config = {.as_uint64 = config},
638 old_config = {.as_uint64 = stimer->config.as_uint64};
639 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
640 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
641
642 if (!synic->active && (!host || config))
643 return 1;
644
645 trace_kvm_hv_stimer_set_config(stimer_to_vcpu(stimer)->vcpu_id,
646 stimer->index, config, host);
647
648 stimer_cleanup(stimer);
649 if (old_config.enable &&
650 !new_config.direct_mode && new_config.sintx == 0)
651 new_config.enable = 0;
652 stimer->config.as_uint64 = new_config.as_uint64;
653
654 if (stimer->config.enable)
655 stimer_mark_pending(stimer, false);
656
657 return 0;
658 }
659
stimer_set_count(struct kvm_vcpu_hv_stimer * stimer,u64 count,bool host)660 static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
661 bool host)
662 {
663 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
664 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
665
666 if (!synic->active && (!host || count))
667 return 1;
668
669 trace_kvm_hv_stimer_set_count(stimer_to_vcpu(stimer)->vcpu_id,
670 stimer->index, count, host);
671
672 stimer_cleanup(stimer);
673 stimer->count = count;
674 if (stimer->count == 0)
675 stimer->config.enable = 0;
676 else if (stimer->config.auto_enable)
677 stimer->config.enable = 1;
678
679 if (stimer->config.enable)
680 stimer_mark_pending(stimer, false);
681
682 return 0;
683 }
684
stimer_get_config(struct kvm_vcpu_hv_stimer * stimer,u64 * pconfig)685 static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
686 {
687 *pconfig = stimer->config.as_uint64;
688 return 0;
689 }
690
stimer_get_count(struct kvm_vcpu_hv_stimer * stimer,u64 * pcount)691 static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
692 {
693 *pcount = stimer->count;
694 return 0;
695 }
696
synic_deliver_msg(struct kvm_vcpu_hv_synic * synic,u32 sint,struct hv_message * src_msg,bool no_retry)697 static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
698 struct hv_message *src_msg, bool no_retry)
699 {
700 struct kvm_vcpu *vcpu = synic_to_vcpu(synic);
701 int msg_off = offsetof(struct hv_message_page, sint_message[sint]);
702 gfn_t msg_page_gfn;
703 struct hv_message_header hv_hdr;
704 int r;
705
706 if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
707 return -ENOENT;
708
709 msg_page_gfn = synic->msg_page >> PAGE_SHIFT;
710
711 /*
712 * Strictly following the spec-mandated ordering would assume setting
713 * .msg_pending before checking .message_type. However, this function
714 * is only called in vcpu context so the entire update is atomic from
715 * guest POV and thus the exact order here doesn't matter.
716 */
717 r = kvm_vcpu_read_guest_page(vcpu, msg_page_gfn, &hv_hdr.message_type,
718 msg_off + offsetof(struct hv_message,
719 header.message_type),
720 sizeof(hv_hdr.message_type));
721 if (r < 0)
722 return r;
723
724 if (hv_hdr.message_type != HVMSG_NONE) {
725 if (no_retry)
726 return 0;
727
728 hv_hdr.message_flags.msg_pending = 1;
729 r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn,
730 &hv_hdr.message_flags,
731 msg_off +
732 offsetof(struct hv_message,
733 header.message_flags),
734 sizeof(hv_hdr.message_flags));
735 if (r < 0)
736 return r;
737 return -EAGAIN;
738 }
739
740 r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn, src_msg, msg_off,
741 sizeof(src_msg->header) +
742 src_msg->header.payload_size);
743 if (r < 0)
744 return r;
745
746 r = synic_set_irq(synic, sint);
747 if (r < 0)
748 return r;
749 if (r == 0)
750 return -EFAULT;
751 return 0;
752 }
753
stimer_send_msg(struct kvm_vcpu_hv_stimer * stimer)754 static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
755 {
756 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
757 struct hv_message *msg = &stimer->msg;
758 struct hv_timer_message_payload *payload =
759 (struct hv_timer_message_payload *)&msg->u.payload;
760
761 /*
762 * To avoid piling up periodic ticks, don't retry message
763 * delivery for them (within "lazy" lost ticks policy).
764 */
765 bool no_retry = stimer->config.periodic;
766
767 payload->expiration_time = stimer->exp_time;
768 payload->delivery_time = get_time_ref_counter(vcpu->kvm);
769 return synic_deliver_msg(vcpu_to_synic(vcpu),
770 stimer->config.sintx, msg,
771 no_retry);
772 }
773
stimer_notify_direct(struct kvm_vcpu_hv_stimer * stimer)774 static int stimer_notify_direct(struct kvm_vcpu_hv_stimer *stimer)
775 {
776 struct kvm_vcpu *vcpu = stimer_to_vcpu(stimer);
777 struct kvm_lapic_irq irq = {
778 .delivery_mode = APIC_DM_FIXED,
779 .vector = stimer->config.apic_vector
780 };
781
782 if (lapic_in_kernel(vcpu))
783 return !kvm_apic_set_irq(vcpu, &irq, NULL);
784 return 0;
785 }
786
stimer_expiration(struct kvm_vcpu_hv_stimer * stimer)787 static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
788 {
789 int r, direct = stimer->config.direct_mode;
790
791 stimer->msg_pending = true;
792 if (!direct)
793 r = stimer_send_msg(stimer);
794 else
795 r = stimer_notify_direct(stimer);
796 trace_kvm_hv_stimer_expiration(stimer_to_vcpu(stimer)->vcpu_id,
797 stimer->index, direct, r);
798 if (!r) {
799 stimer->msg_pending = false;
800 if (!(stimer->config.periodic))
801 stimer->config.enable = 0;
802 }
803 }
804
kvm_hv_process_stimers(struct kvm_vcpu * vcpu)805 void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
806 {
807 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
808 struct kvm_vcpu_hv_stimer *stimer;
809 u64 time_now, exp_time;
810 int i;
811
812 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
813 if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
814 stimer = &hv_vcpu->stimer[i];
815 if (stimer->config.enable) {
816 exp_time = stimer->exp_time;
817
818 if (exp_time) {
819 time_now =
820 get_time_ref_counter(vcpu->kvm);
821 if (time_now >= exp_time)
822 stimer_expiration(stimer);
823 }
824
825 if ((stimer->config.enable) &&
826 stimer->count) {
827 if (!stimer->msg_pending)
828 stimer_start(stimer);
829 } else
830 stimer_cleanup(stimer);
831 }
832 }
833 }
834
kvm_hv_vcpu_uninit(struct kvm_vcpu * vcpu)835 void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)
836 {
837 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
838 int i;
839
840 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
841 stimer_cleanup(&hv_vcpu->stimer[i]);
842 }
843
kvm_hv_assist_page_enabled(struct kvm_vcpu * vcpu)844 bool kvm_hv_assist_page_enabled(struct kvm_vcpu *vcpu)
845 {
846 if (!(vcpu->arch.hyperv.hv_vapic & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE))
847 return false;
848 return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
849 }
850 EXPORT_SYMBOL_GPL(kvm_hv_assist_page_enabled);
851
kvm_hv_get_assist_page(struct kvm_vcpu * vcpu,struct hv_vp_assist_page * assist_page)852 bool kvm_hv_get_assist_page(struct kvm_vcpu *vcpu,
853 struct hv_vp_assist_page *assist_page)
854 {
855 if (!kvm_hv_assist_page_enabled(vcpu))
856 return false;
857 return !kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data,
858 assist_page, sizeof(*assist_page));
859 }
860 EXPORT_SYMBOL_GPL(kvm_hv_get_assist_page);
861
stimer_prepare_msg(struct kvm_vcpu_hv_stimer * stimer)862 static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer)
863 {
864 struct hv_message *msg = &stimer->msg;
865 struct hv_timer_message_payload *payload =
866 (struct hv_timer_message_payload *)&msg->u.payload;
867
868 memset(&msg->header, 0, sizeof(msg->header));
869 msg->header.message_type = HVMSG_TIMER_EXPIRED;
870 msg->header.payload_size = sizeof(*payload);
871
872 payload->timer_index = stimer->index;
873 payload->expiration_time = 0;
874 payload->delivery_time = 0;
875 }
876
stimer_init(struct kvm_vcpu_hv_stimer * stimer,int timer_index)877 static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index)
878 {
879 memset(stimer, 0, sizeof(*stimer));
880 stimer->index = timer_index;
881 hrtimer_init(&stimer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
882 stimer->timer.function = stimer_timer_callback;
883 stimer_prepare_msg(stimer);
884 }
885
kvm_hv_vcpu_init(struct kvm_vcpu * vcpu)886 void kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
887 {
888 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
889 int i;
890
891 synic_init(&hv_vcpu->synic);
892
893 bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
894 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
895 stimer_init(&hv_vcpu->stimer[i], i);
896 }
897
kvm_hv_vcpu_postcreate(struct kvm_vcpu * vcpu)898 void kvm_hv_vcpu_postcreate(struct kvm_vcpu *vcpu)
899 {
900 struct kvm_vcpu_hv *hv_vcpu = vcpu_to_hv_vcpu(vcpu);
901
902 hv_vcpu->vp_index = kvm_vcpu_get_idx(vcpu);
903 }
904
kvm_hv_activate_synic(struct kvm_vcpu * vcpu,bool dont_zero_synic_pages)905 int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages)
906 {
907 struct kvm_vcpu_hv_synic *synic = vcpu_to_synic(vcpu);
908
909 /*
910 * Hyper-V SynIC auto EOI SINT's are
911 * not compatible with APICV, so request
912 * to deactivate APICV permanently.
913 */
914 kvm_request_apicv_update(vcpu->kvm, false, APICV_INHIBIT_REASON_HYPERV);
915 synic->active = true;
916 synic->dont_zero_synic_pages = dont_zero_synic_pages;
917 synic->control = HV_SYNIC_CONTROL_ENABLE;
918 return 0;
919 }
920
kvm_hv_msr_partition_wide(u32 msr)921 static bool kvm_hv_msr_partition_wide(u32 msr)
922 {
923 bool r = false;
924
925 switch (msr) {
926 case HV_X64_MSR_GUEST_OS_ID:
927 case HV_X64_MSR_HYPERCALL:
928 case HV_X64_MSR_REFERENCE_TSC:
929 case HV_X64_MSR_TIME_REF_COUNT:
930 case HV_X64_MSR_CRASH_CTL:
931 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
932 case HV_X64_MSR_RESET:
933 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
934 case HV_X64_MSR_TSC_EMULATION_CONTROL:
935 case HV_X64_MSR_TSC_EMULATION_STATUS:
936 case HV_X64_MSR_SYNDBG_OPTIONS:
937 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
938 r = true;
939 break;
940 }
941
942 return r;
943 }
944
kvm_hv_msr_get_crash_data(struct kvm_vcpu * vcpu,u32 index,u64 * pdata)945 static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
946 u32 index, u64 *pdata)
947 {
948 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
949 size_t size = ARRAY_SIZE(hv->hv_crash_param);
950
951 if (WARN_ON_ONCE(index >= size))
952 return -EINVAL;
953
954 *pdata = hv->hv_crash_param[array_index_nospec(index, size)];
955 return 0;
956 }
957
kvm_hv_msr_get_crash_ctl(struct kvm_vcpu * vcpu,u64 * pdata)958 static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu *vcpu, u64 *pdata)
959 {
960 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
961
962 *pdata = hv->hv_crash_ctl;
963 return 0;
964 }
965
kvm_hv_msr_set_crash_ctl(struct kvm_vcpu * vcpu,u64 data,bool host)966 static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu *vcpu, u64 data, bool host)
967 {
968 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
969
970 if (host)
971 hv->hv_crash_ctl = data & HV_CRASH_CTL_CRASH_NOTIFY;
972
973 if (!host && (data & HV_CRASH_CTL_CRASH_NOTIFY)) {
974
975 vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
976 hv->hv_crash_param[0],
977 hv->hv_crash_param[1],
978 hv->hv_crash_param[2],
979 hv->hv_crash_param[3],
980 hv->hv_crash_param[4]);
981
982 /* Send notification about crash to user space */
983 kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
984 }
985
986 return 0;
987 }
988
kvm_hv_msr_set_crash_data(struct kvm_vcpu * vcpu,u32 index,u64 data)989 static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
990 u32 index, u64 data)
991 {
992 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
993 size_t size = ARRAY_SIZE(hv->hv_crash_param);
994
995 if (WARN_ON_ONCE(index >= size))
996 return -EINVAL;
997
998 hv->hv_crash_param[array_index_nospec(index, size)] = data;
999 return 0;
1000 }
1001
1002 /*
1003 * The kvmclock and Hyper-V TSC page use similar formulas, and converting
1004 * between them is possible:
1005 *
1006 * kvmclock formula:
1007 * nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32)
1008 * + system_time
1009 *
1010 * Hyper-V formula:
1011 * nsec/100 = ticks * scale / 2^64 + offset
1012 *
1013 * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula.
1014 * By dividing the kvmclock formula by 100 and equating what's left we get:
1015 * ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
1016 * scale / 2^64 = tsc_to_system_mul * 2^(tsc_shift-32) / 100
1017 * scale = tsc_to_system_mul * 2^(32+tsc_shift) / 100
1018 *
1019 * Now expand the kvmclock formula and divide by 100:
1020 * nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32)
1021 * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32)
1022 * + system_time
1023 * nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
1024 * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100
1025 * + system_time / 100
1026 *
1027 * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64:
1028 * nsec/100 = ticks * scale / 2^64
1029 * - tsc_timestamp * scale / 2^64
1030 * + system_time / 100
1031 *
1032 * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out:
1033 * offset = system_time / 100 - tsc_timestamp * scale / 2^64
1034 *
1035 * These two equivalencies are implemented in this function.
1036 */
compute_tsc_page_parameters(struct pvclock_vcpu_time_info * hv_clock,struct ms_hyperv_tsc_page * tsc_ref)1037 static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock,
1038 struct ms_hyperv_tsc_page *tsc_ref)
1039 {
1040 u64 max_mul;
1041
1042 if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT))
1043 return false;
1044
1045 /*
1046 * check if scale would overflow, if so we use the time ref counter
1047 * tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64
1048 * tsc_to_system_mul / 100 >= 2^(32-tsc_shift)
1049 * tsc_to_system_mul >= 100 * 2^(32-tsc_shift)
1050 */
1051 max_mul = 100ull << (32 - hv_clock->tsc_shift);
1052 if (hv_clock->tsc_to_system_mul >= max_mul)
1053 return false;
1054
1055 /*
1056 * Otherwise compute the scale and offset according to the formulas
1057 * derived above.
1058 */
1059 tsc_ref->tsc_scale =
1060 mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift),
1061 hv_clock->tsc_to_system_mul,
1062 100);
1063
1064 tsc_ref->tsc_offset = hv_clock->system_time;
1065 do_div(tsc_ref->tsc_offset, 100);
1066 tsc_ref->tsc_offset -=
1067 mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64);
1068 return true;
1069 }
1070
kvm_hv_setup_tsc_page(struct kvm * kvm,struct pvclock_vcpu_time_info * hv_clock)1071 void kvm_hv_setup_tsc_page(struct kvm *kvm,
1072 struct pvclock_vcpu_time_info *hv_clock)
1073 {
1074 struct kvm_hv *hv = &kvm->arch.hyperv;
1075 u32 tsc_seq;
1076 u64 gfn;
1077
1078 BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence));
1079 BUILD_BUG_ON(offsetof(struct ms_hyperv_tsc_page, tsc_sequence) != 0);
1080
1081 if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
1082 return;
1083
1084 mutex_lock(&kvm->arch.hyperv.hv_lock);
1085 if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
1086 goto out_unlock;
1087
1088 gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
1089 /*
1090 * Because the TSC parameters only vary when there is a
1091 * change in the master clock, do not bother with caching.
1092 */
1093 if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn),
1094 &tsc_seq, sizeof(tsc_seq))))
1095 goto out_unlock;
1096
1097 /*
1098 * While we're computing and writing the parameters, force the
1099 * guest to use the time reference count MSR.
1100 */
1101 hv->tsc_ref.tsc_sequence = 0;
1102 if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
1103 &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
1104 goto out_unlock;
1105
1106 if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref))
1107 goto out_unlock;
1108
1109 /* Ensure sequence is zero before writing the rest of the struct. */
1110 smp_wmb();
1111 if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
1112 goto out_unlock;
1113
1114 /*
1115 * Now switch to the TSC page mechanism by writing the sequence.
1116 */
1117 tsc_seq++;
1118 if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0)
1119 tsc_seq = 1;
1120
1121 /* Write the struct entirely before the non-zero sequence. */
1122 smp_wmb();
1123
1124 hv->tsc_ref.tsc_sequence = tsc_seq;
1125 kvm_write_guest(kvm, gfn_to_gpa(gfn),
1126 &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence));
1127 out_unlock:
1128 mutex_unlock(&kvm->arch.hyperv.hv_lock);
1129 }
1130
kvm_hv_set_msr_pw(struct kvm_vcpu * vcpu,u32 msr,u64 data,bool host)1131 static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
1132 bool host)
1133 {
1134 struct kvm *kvm = vcpu->kvm;
1135 struct kvm_hv *hv = &kvm->arch.hyperv;
1136
1137 switch (msr) {
1138 case HV_X64_MSR_GUEST_OS_ID:
1139 hv->hv_guest_os_id = data;
1140 /* setting guest os id to zero disables hypercall page */
1141 if (!hv->hv_guest_os_id)
1142 hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
1143 break;
1144 case HV_X64_MSR_HYPERCALL: {
1145 u64 gfn;
1146 unsigned long addr;
1147 u8 instructions[4];
1148
1149 /* if guest os id is not set hypercall should remain disabled */
1150 if (!hv->hv_guest_os_id)
1151 break;
1152 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
1153 hv->hv_hypercall = data;
1154 break;
1155 }
1156 gfn = data >> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT;
1157 addr = gfn_to_hva(kvm, gfn);
1158 if (kvm_is_error_hva(addr))
1159 return 1;
1160 kvm_x86_ops.patch_hypercall(vcpu, instructions);
1161 ((unsigned char *)instructions)[3] = 0xc3; /* ret */
1162 if (__copy_to_user((void __user *)addr, instructions, 4))
1163 return 1;
1164 hv->hv_hypercall = data;
1165 mark_page_dirty(kvm, gfn);
1166 break;
1167 }
1168 case HV_X64_MSR_REFERENCE_TSC:
1169 hv->hv_tsc_page = data;
1170 if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE)
1171 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1172 break;
1173 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1174 return kvm_hv_msr_set_crash_data(vcpu,
1175 msr - HV_X64_MSR_CRASH_P0,
1176 data);
1177 case HV_X64_MSR_CRASH_CTL:
1178 return kvm_hv_msr_set_crash_ctl(vcpu, data, host);
1179 case HV_X64_MSR_RESET:
1180 if (data == 1) {
1181 vcpu_debug(vcpu, "hyper-v reset requested\n");
1182 kvm_make_request(KVM_REQ_HV_RESET, vcpu);
1183 }
1184 break;
1185 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1186 hv->hv_reenlightenment_control = data;
1187 break;
1188 case HV_X64_MSR_TSC_EMULATION_CONTROL:
1189 hv->hv_tsc_emulation_control = data;
1190 break;
1191 case HV_X64_MSR_TSC_EMULATION_STATUS:
1192 hv->hv_tsc_emulation_status = data;
1193 break;
1194 case HV_X64_MSR_TIME_REF_COUNT:
1195 /* read-only, but still ignore it if host-initiated */
1196 if (!host)
1197 return 1;
1198 break;
1199 case HV_X64_MSR_SYNDBG_OPTIONS:
1200 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
1201 return syndbg_set_msr(vcpu, msr, data, host);
1202 default:
1203 vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n",
1204 msr, data);
1205 return 1;
1206 }
1207 return 0;
1208 }
1209
1210 /* Calculate cpu time spent by current task in 100ns units */
current_task_runtime_100ns(void)1211 static u64 current_task_runtime_100ns(void)
1212 {
1213 u64 utime, stime;
1214
1215 task_cputime_adjusted(current, &utime, &stime);
1216
1217 return div_u64(utime + stime, 100);
1218 }
1219
kvm_hv_set_msr(struct kvm_vcpu * vcpu,u32 msr,u64 data,bool host)1220 static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
1221 {
1222 struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
1223
1224 switch (msr) {
1225 case HV_X64_MSR_VP_INDEX: {
1226 struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
1227 int vcpu_idx = kvm_vcpu_get_idx(vcpu);
1228 u32 new_vp_index = (u32)data;
1229
1230 if (!host || new_vp_index >= KVM_MAX_VCPUS)
1231 return 1;
1232
1233 if (new_vp_index == hv_vcpu->vp_index)
1234 return 0;
1235
1236 /*
1237 * The VP index is initialized to vcpu_index by
1238 * kvm_hv_vcpu_postcreate so they initially match. Now the
1239 * VP index is changing, adjust num_mismatched_vp_indexes if
1240 * it now matches or no longer matches vcpu_idx.
1241 */
1242 if (hv_vcpu->vp_index == vcpu_idx)
1243 atomic_inc(&hv->num_mismatched_vp_indexes);
1244 else if (new_vp_index == vcpu_idx)
1245 atomic_dec(&hv->num_mismatched_vp_indexes);
1246
1247 hv_vcpu->vp_index = new_vp_index;
1248 break;
1249 }
1250 case HV_X64_MSR_VP_ASSIST_PAGE: {
1251 u64 gfn;
1252 unsigned long addr;
1253
1254 if (!(data & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE)) {
1255 hv_vcpu->hv_vapic = data;
1256 if (kvm_lapic_enable_pv_eoi(vcpu, 0, 0))
1257 return 1;
1258 break;
1259 }
1260 gfn = data >> HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT;
1261 addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
1262 if (kvm_is_error_hva(addr))
1263 return 1;
1264
1265 /*
1266 * Clear apic_assist portion of struct hv_vp_assist_page
1267 * only, there can be valuable data in the rest which needs
1268 * to be preserved e.g. on migration.
1269 */
1270 if (__put_user(0, (u32 __user *)addr))
1271 return 1;
1272 hv_vcpu->hv_vapic = data;
1273 kvm_vcpu_mark_page_dirty(vcpu, gfn);
1274 if (kvm_lapic_enable_pv_eoi(vcpu,
1275 gfn_to_gpa(gfn) | KVM_MSR_ENABLED,
1276 sizeof(struct hv_vp_assist_page)))
1277 return 1;
1278 break;
1279 }
1280 case HV_X64_MSR_EOI:
1281 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1282 case HV_X64_MSR_ICR:
1283 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1284 case HV_X64_MSR_TPR:
1285 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
1286 case HV_X64_MSR_VP_RUNTIME:
1287 if (!host)
1288 return 1;
1289 hv_vcpu->runtime_offset = data - current_task_runtime_100ns();
1290 break;
1291 case HV_X64_MSR_SCONTROL:
1292 case HV_X64_MSR_SVERSION:
1293 case HV_X64_MSR_SIEFP:
1294 case HV_X64_MSR_SIMP:
1295 case HV_X64_MSR_EOM:
1296 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
1297 return synic_set_msr(vcpu_to_synic(vcpu), msr, data, host);
1298 case HV_X64_MSR_STIMER0_CONFIG:
1299 case HV_X64_MSR_STIMER1_CONFIG:
1300 case HV_X64_MSR_STIMER2_CONFIG:
1301 case HV_X64_MSR_STIMER3_CONFIG: {
1302 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
1303
1304 return stimer_set_config(vcpu_to_stimer(vcpu, timer_index),
1305 data, host);
1306 }
1307 case HV_X64_MSR_STIMER0_COUNT:
1308 case HV_X64_MSR_STIMER1_COUNT:
1309 case HV_X64_MSR_STIMER2_COUNT:
1310 case HV_X64_MSR_STIMER3_COUNT: {
1311 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
1312
1313 return stimer_set_count(vcpu_to_stimer(vcpu, timer_index),
1314 data, host);
1315 }
1316 case HV_X64_MSR_TSC_FREQUENCY:
1317 case HV_X64_MSR_APIC_FREQUENCY:
1318 /* read-only, but still ignore it if host-initiated */
1319 if (!host)
1320 return 1;
1321 break;
1322 default:
1323 vcpu_unimpl(vcpu, "Hyper-V unhandled wrmsr: 0x%x data 0x%llx\n",
1324 msr, data);
1325 return 1;
1326 }
1327
1328 return 0;
1329 }
1330
kvm_hv_get_msr_pw(struct kvm_vcpu * vcpu,u32 msr,u64 * pdata,bool host)1331 static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,
1332 bool host)
1333 {
1334 u64 data = 0;
1335 struct kvm *kvm = vcpu->kvm;
1336 struct kvm_hv *hv = &kvm->arch.hyperv;
1337
1338 switch (msr) {
1339 case HV_X64_MSR_GUEST_OS_ID:
1340 data = hv->hv_guest_os_id;
1341 break;
1342 case HV_X64_MSR_HYPERCALL:
1343 data = hv->hv_hypercall;
1344 break;
1345 case HV_X64_MSR_TIME_REF_COUNT:
1346 data = get_time_ref_counter(kvm);
1347 break;
1348 case HV_X64_MSR_REFERENCE_TSC:
1349 data = hv->hv_tsc_page;
1350 break;
1351 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1352 return kvm_hv_msr_get_crash_data(vcpu,
1353 msr - HV_X64_MSR_CRASH_P0,
1354 pdata);
1355 case HV_X64_MSR_CRASH_CTL:
1356 return kvm_hv_msr_get_crash_ctl(vcpu, pdata);
1357 case HV_X64_MSR_RESET:
1358 data = 0;
1359 break;
1360 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1361 data = hv->hv_reenlightenment_control;
1362 break;
1363 case HV_X64_MSR_TSC_EMULATION_CONTROL:
1364 data = hv->hv_tsc_emulation_control;
1365 break;
1366 case HV_X64_MSR_TSC_EMULATION_STATUS:
1367 data = hv->hv_tsc_emulation_status;
1368 break;
1369 case HV_X64_MSR_SYNDBG_OPTIONS:
1370 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
1371 return syndbg_get_msr(vcpu, msr, pdata, host);
1372 default:
1373 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1374 return 1;
1375 }
1376
1377 *pdata = data;
1378 return 0;
1379 }
1380
kvm_hv_get_msr(struct kvm_vcpu * vcpu,u32 msr,u64 * pdata,bool host)1381 static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,
1382 bool host)
1383 {
1384 u64 data = 0;
1385 struct kvm_vcpu_hv *hv_vcpu = &vcpu->arch.hyperv;
1386
1387 switch (msr) {
1388 case HV_X64_MSR_VP_INDEX:
1389 data = hv_vcpu->vp_index;
1390 break;
1391 case HV_X64_MSR_EOI:
1392 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1393 case HV_X64_MSR_ICR:
1394 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1395 case HV_X64_MSR_TPR:
1396 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
1397 case HV_X64_MSR_VP_ASSIST_PAGE:
1398 data = hv_vcpu->hv_vapic;
1399 break;
1400 case HV_X64_MSR_VP_RUNTIME:
1401 data = current_task_runtime_100ns() + hv_vcpu->runtime_offset;
1402 break;
1403 case HV_X64_MSR_SCONTROL:
1404 case HV_X64_MSR_SVERSION:
1405 case HV_X64_MSR_SIEFP:
1406 case HV_X64_MSR_SIMP:
1407 case HV_X64_MSR_EOM:
1408 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
1409 return synic_get_msr(vcpu_to_synic(vcpu), msr, pdata, host);
1410 case HV_X64_MSR_STIMER0_CONFIG:
1411 case HV_X64_MSR_STIMER1_CONFIG:
1412 case HV_X64_MSR_STIMER2_CONFIG:
1413 case HV_X64_MSR_STIMER3_CONFIG: {
1414 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
1415
1416 return stimer_get_config(vcpu_to_stimer(vcpu, timer_index),
1417 pdata);
1418 }
1419 case HV_X64_MSR_STIMER0_COUNT:
1420 case HV_X64_MSR_STIMER1_COUNT:
1421 case HV_X64_MSR_STIMER2_COUNT:
1422 case HV_X64_MSR_STIMER3_COUNT: {
1423 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
1424
1425 return stimer_get_count(vcpu_to_stimer(vcpu, timer_index),
1426 pdata);
1427 }
1428 case HV_X64_MSR_TSC_FREQUENCY:
1429 data = (u64)vcpu->arch.virtual_tsc_khz * 1000;
1430 break;
1431 case HV_X64_MSR_APIC_FREQUENCY:
1432 data = APIC_BUS_FREQUENCY;
1433 break;
1434 default:
1435 vcpu_unimpl(vcpu, "Hyper-V unhandled rdmsr: 0x%x\n", msr);
1436 return 1;
1437 }
1438 *pdata = data;
1439 return 0;
1440 }
1441
kvm_hv_set_msr_common(struct kvm_vcpu * vcpu,u32 msr,u64 data,bool host)1442 int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
1443 {
1444 if (kvm_hv_msr_partition_wide(msr)) {
1445 int r;
1446
1447 mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
1448 r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
1449 mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
1450 return r;
1451 } else
1452 return kvm_hv_set_msr(vcpu, msr, data, host);
1453 }
1454
kvm_hv_get_msr_common(struct kvm_vcpu * vcpu,u32 msr,u64 * pdata,bool host)1455 int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
1456 {
1457 if (kvm_hv_msr_partition_wide(msr)) {
1458 int r;
1459
1460 mutex_lock(&vcpu->kvm->arch.hyperv.hv_lock);
1461 r = kvm_hv_get_msr_pw(vcpu, msr, pdata, host);
1462 mutex_unlock(&vcpu->kvm->arch.hyperv.hv_lock);
1463 return r;
1464 } else
1465 return kvm_hv_get_msr(vcpu, msr, pdata, host);
1466 }
1467
sparse_set_to_vcpu_mask(struct kvm * kvm,u64 * sparse_banks,u64 valid_bank_mask,u64 * vp_bitmap,unsigned long * vcpu_bitmap)1468 static __always_inline unsigned long *sparse_set_to_vcpu_mask(
1469 struct kvm *kvm, u64 *sparse_banks, u64 valid_bank_mask,
1470 u64 *vp_bitmap, unsigned long *vcpu_bitmap)
1471 {
1472 struct kvm_hv *hv = &kvm->arch.hyperv;
1473 struct kvm_vcpu *vcpu;
1474 int i, bank, sbank = 0;
1475
1476 memset(vp_bitmap, 0,
1477 KVM_HV_MAX_SPARSE_VCPU_SET_BITS * sizeof(*vp_bitmap));
1478 for_each_set_bit(bank, (unsigned long *)&valid_bank_mask,
1479 KVM_HV_MAX_SPARSE_VCPU_SET_BITS)
1480 vp_bitmap[bank] = sparse_banks[sbank++];
1481
1482 if (likely(!atomic_read(&hv->num_mismatched_vp_indexes))) {
1483 /* for all vcpus vp_index == vcpu_idx */
1484 return (unsigned long *)vp_bitmap;
1485 }
1486
1487 bitmap_zero(vcpu_bitmap, KVM_MAX_VCPUS);
1488 kvm_for_each_vcpu(i, vcpu, kvm) {
1489 if (test_bit(vcpu_to_hv_vcpu(vcpu)->vp_index,
1490 (unsigned long *)vp_bitmap))
1491 __set_bit(i, vcpu_bitmap);
1492 }
1493 return vcpu_bitmap;
1494 }
1495
kvm_hv_flush_tlb(struct kvm_vcpu * current_vcpu,u64 ingpa,u16 rep_cnt,bool ex)1496 static u64 kvm_hv_flush_tlb(struct kvm_vcpu *current_vcpu, u64 ingpa,
1497 u16 rep_cnt, bool ex)
1498 {
1499 struct kvm *kvm = current_vcpu->kvm;
1500 struct kvm_vcpu_hv *hv_vcpu = ¤t_vcpu->arch.hyperv;
1501 struct hv_tlb_flush_ex flush_ex;
1502 struct hv_tlb_flush flush;
1503 u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS];
1504 DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS);
1505 unsigned long *vcpu_mask;
1506 u64 valid_bank_mask;
1507 u64 sparse_banks[64];
1508 int sparse_banks_len;
1509 bool all_cpus;
1510
1511 if (!ex) {
1512 if (unlikely(kvm_read_guest(kvm, ingpa, &flush, sizeof(flush))))
1513 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1514
1515 trace_kvm_hv_flush_tlb(flush.processor_mask,
1516 flush.address_space, flush.flags);
1517
1518 valid_bank_mask = BIT_ULL(0);
1519 sparse_banks[0] = flush.processor_mask;
1520
1521 /*
1522 * Work around possible WS2012 bug: it sends hypercalls
1523 * with processor_mask = 0x0 and HV_FLUSH_ALL_PROCESSORS clear,
1524 * while also expecting us to flush something and crashing if
1525 * we don't. Let's treat processor_mask == 0 same as
1526 * HV_FLUSH_ALL_PROCESSORS.
1527 */
1528 all_cpus = (flush.flags & HV_FLUSH_ALL_PROCESSORS) ||
1529 flush.processor_mask == 0;
1530 } else {
1531 if (unlikely(kvm_read_guest(kvm, ingpa, &flush_ex,
1532 sizeof(flush_ex))))
1533 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1534
1535 trace_kvm_hv_flush_tlb_ex(flush_ex.hv_vp_set.valid_bank_mask,
1536 flush_ex.hv_vp_set.format,
1537 flush_ex.address_space,
1538 flush_ex.flags);
1539
1540 valid_bank_mask = flush_ex.hv_vp_set.valid_bank_mask;
1541 all_cpus = flush_ex.hv_vp_set.format !=
1542 HV_GENERIC_SET_SPARSE_4K;
1543
1544 sparse_banks_len =
1545 bitmap_weight((unsigned long *)&valid_bank_mask, 64) *
1546 sizeof(sparse_banks[0]);
1547
1548 if (!sparse_banks_len && !all_cpus)
1549 goto ret_success;
1550
1551 if (!all_cpus &&
1552 kvm_read_guest(kvm,
1553 ingpa + offsetof(struct hv_tlb_flush_ex,
1554 hv_vp_set.bank_contents),
1555 sparse_banks,
1556 sparse_banks_len))
1557 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1558 }
1559
1560 cpumask_clear(&hv_vcpu->tlb_flush);
1561
1562 vcpu_mask = all_cpus ? NULL :
1563 sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask,
1564 vp_bitmap, vcpu_bitmap);
1565
1566 /*
1567 * vcpu->arch.cr3 may not be up-to-date for running vCPUs so we can't
1568 * analyze it here, flush TLB regardless of the specified address space.
1569 */
1570 kvm_make_vcpus_request_mask(kvm, KVM_REQ_TLB_FLUSH_GUEST,
1571 NULL, vcpu_mask, &hv_vcpu->tlb_flush);
1572
1573 ret_success:
1574 /* We always do full TLB flush, set rep_done = rep_cnt. */
1575 return (u64)HV_STATUS_SUCCESS |
1576 ((u64)rep_cnt << HV_HYPERCALL_REP_COMP_OFFSET);
1577 }
1578
kvm_send_ipi_to_many(struct kvm * kvm,u32 vector,unsigned long * vcpu_bitmap)1579 static void kvm_send_ipi_to_many(struct kvm *kvm, u32 vector,
1580 unsigned long *vcpu_bitmap)
1581 {
1582 struct kvm_lapic_irq irq = {
1583 .delivery_mode = APIC_DM_FIXED,
1584 .vector = vector
1585 };
1586 struct kvm_vcpu *vcpu;
1587 int i;
1588
1589 kvm_for_each_vcpu(i, vcpu, kvm) {
1590 if (vcpu_bitmap && !test_bit(i, vcpu_bitmap))
1591 continue;
1592
1593 /* We fail only when APIC is disabled */
1594 kvm_apic_set_irq(vcpu, &irq, NULL);
1595 }
1596 }
1597
kvm_hv_send_ipi(struct kvm_vcpu * current_vcpu,u64 ingpa,u64 outgpa,bool ex,bool fast)1598 static u64 kvm_hv_send_ipi(struct kvm_vcpu *current_vcpu, u64 ingpa, u64 outgpa,
1599 bool ex, bool fast)
1600 {
1601 struct kvm *kvm = current_vcpu->kvm;
1602 struct hv_send_ipi_ex send_ipi_ex;
1603 struct hv_send_ipi send_ipi;
1604 u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS];
1605 DECLARE_BITMAP(vcpu_bitmap, KVM_MAX_VCPUS);
1606 unsigned long *vcpu_mask;
1607 unsigned long valid_bank_mask;
1608 u64 sparse_banks[64];
1609 int sparse_banks_len;
1610 u32 vector;
1611 bool all_cpus;
1612
1613 if (!ex) {
1614 if (!fast) {
1615 if (unlikely(kvm_read_guest(kvm, ingpa, &send_ipi,
1616 sizeof(send_ipi))))
1617 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1618 sparse_banks[0] = send_ipi.cpu_mask;
1619 vector = send_ipi.vector;
1620 } else {
1621 /* 'reserved' part of hv_send_ipi should be 0 */
1622 if (unlikely(ingpa >> 32 != 0))
1623 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1624 sparse_banks[0] = outgpa;
1625 vector = (u32)ingpa;
1626 }
1627 all_cpus = false;
1628 valid_bank_mask = BIT_ULL(0);
1629
1630 trace_kvm_hv_send_ipi(vector, sparse_banks[0]);
1631 } else {
1632 if (unlikely(kvm_read_guest(kvm, ingpa, &send_ipi_ex,
1633 sizeof(send_ipi_ex))))
1634 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1635
1636 trace_kvm_hv_send_ipi_ex(send_ipi_ex.vector,
1637 send_ipi_ex.vp_set.format,
1638 send_ipi_ex.vp_set.valid_bank_mask);
1639
1640 vector = send_ipi_ex.vector;
1641 valid_bank_mask = send_ipi_ex.vp_set.valid_bank_mask;
1642 sparse_banks_len = bitmap_weight(&valid_bank_mask, 64) *
1643 sizeof(sparse_banks[0]);
1644
1645 all_cpus = send_ipi_ex.vp_set.format == HV_GENERIC_SET_ALL;
1646
1647 if (all_cpus)
1648 goto check_and_send_ipi;
1649
1650 if (!sparse_banks_len)
1651 goto ret_success;
1652
1653 if (kvm_read_guest(kvm,
1654 ingpa + offsetof(struct hv_send_ipi_ex,
1655 vp_set.bank_contents),
1656 sparse_banks,
1657 sparse_banks_len))
1658 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1659 }
1660
1661 check_and_send_ipi:
1662 if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR))
1663 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1664
1665 vcpu_mask = all_cpus ? NULL :
1666 sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask,
1667 vp_bitmap, vcpu_bitmap);
1668
1669 kvm_send_ipi_to_many(kvm, vector, vcpu_mask);
1670
1671 ret_success:
1672 return HV_STATUS_SUCCESS;
1673 }
1674
kvm_hv_hypercall_enabled(struct kvm * kvm)1675 bool kvm_hv_hypercall_enabled(struct kvm *kvm)
1676 {
1677 return READ_ONCE(kvm->arch.hyperv.hv_guest_os_id) != 0;
1678 }
1679
kvm_hv_hypercall_set_result(struct kvm_vcpu * vcpu,u64 result)1680 static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
1681 {
1682 bool longmode;
1683
1684 longmode = is_64_bit_mode(vcpu);
1685 if (longmode)
1686 kvm_rax_write(vcpu, result);
1687 else {
1688 kvm_rdx_write(vcpu, result >> 32);
1689 kvm_rax_write(vcpu, result & 0xffffffff);
1690 }
1691 }
1692
kvm_hv_hypercall_complete(struct kvm_vcpu * vcpu,u64 result)1693 static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result)
1694 {
1695 kvm_hv_hypercall_set_result(vcpu, result);
1696 ++vcpu->stat.hypercalls;
1697 return kvm_skip_emulated_instruction(vcpu);
1698 }
1699
kvm_hv_hypercall_complete_userspace(struct kvm_vcpu * vcpu)1700 static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
1701 {
1702 return kvm_hv_hypercall_complete(vcpu, vcpu->run->hyperv.u.hcall.result);
1703 }
1704
kvm_hvcall_signal_event(struct kvm_vcpu * vcpu,bool fast,u64 param)1705 static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, bool fast, u64 param)
1706 {
1707 struct eventfd_ctx *eventfd;
1708
1709 if (unlikely(!fast)) {
1710 int ret;
1711 gpa_t gpa = param;
1712
1713 if ((gpa & (__alignof__(param) - 1)) ||
1714 offset_in_page(gpa) + sizeof(param) > PAGE_SIZE)
1715 return HV_STATUS_INVALID_ALIGNMENT;
1716
1717 ret = kvm_vcpu_read_guest(vcpu, gpa, ¶m, sizeof(param));
1718 if (ret < 0)
1719 return HV_STATUS_INVALID_ALIGNMENT;
1720 }
1721
1722 /*
1723 * Per spec, bits 32-47 contain the extra "flag number". However, we
1724 * have no use for it, and in all known usecases it is zero, so just
1725 * report lookup failure if it isn't.
1726 */
1727 if (param & 0xffff00000000ULL)
1728 return HV_STATUS_INVALID_PORT_ID;
1729 /* remaining bits are reserved-zero */
1730 if (param & ~KVM_HYPERV_CONN_ID_MASK)
1731 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1732
1733 /* the eventfd is protected by vcpu->kvm->srcu, but conn_to_evt isn't */
1734 rcu_read_lock();
1735 eventfd = idr_find(&vcpu->kvm->arch.hyperv.conn_to_evt, param);
1736 rcu_read_unlock();
1737 if (!eventfd)
1738 return HV_STATUS_INVALID_PORT_ID;
1739
1740 eventfd_signal(eventfd, 1);
1741 return HV_STATUS_SUCCESS;
1742 }
1743
kvm_hv_hypercall(struct kvm_vcpu * vcpu)1744 int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
1745 {
1746 u64 param, ingpa, outgpa, ret = HV_STATUS_SUCCESS;
1747 uint16_t code, rep_idx, rep_cnt;
1748 bool fast, rep;
1749
1750 /*
1751 * hypercall generates UD from non zero cpl and real mode
1752 * per HYPER-V spec
1753 */
1754 if (kvm_x86_ops.get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
1755 kvm_queue_exception(vcpu, UD_VECTOR);
1756 return 1;
1757 }
1758
1759 #ifdef CONFIG_X86_64
1760 if (is_64_bit_mode(vcpu)) {
1761 param = kvm_rcx_read(vcpu);
1762 ingpa = kvm_rdx_read(vcpu);
1763 outgpa = kvm_r8_read(vcpu);
1764 } else
1765 #endif
1766 {
1767 param = ((u64)kvm_rdx_read(vcpu) << 32) |
1768 (kvm_rax_read(vcpu) & 0xffffffff);
1769 ingpa = ((u64)kvm_rbx_read(vcpu) << 32) |
1770 (kvm_rcx_read(vcpu) & 0xffffffff);
1771 outgpa = ((u64)kvm_rdi_read(vcpu) << 32) |
1772 (kvm_rsi_read(vcpu) & 0xffffffff);
1773 }
1774
1775 code = param & 0xffff;
1776 fast = !!(param & HV_HYPERCALL_FAST_BIT);
1777 rep_cnt = (param >> HV_HYPERCALL_REP_COMP_OFFSET) & 0xfff;
1778 rep_idx = (param >> HV_HYPERCALL_REP_START_OFFSET) & 0xfff;
1779 rep = !!(rep_cnt || rep_idx);
1780
1781 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
1782
1783 switch (code) {
1784 case HVCALL_NOTIFY_LONG_SPIN_WAIT:
1785 if (unlikely(rep)) {
1786 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1787 break;
1788 }
1789 kvm_vcpu_on_spin(vcpu, true);
1790 break;
1791 case HVCALL_SIGNAL_EVENT:
1792 if (unlikely(rep)) {
1793 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1794 break;
1795 }
1796 ret = kvm_hvcall_signal_event(vcpu, fast, ingpa);
1797 if (ret != HV_STATUS_INVALID_PORT_ID)
1798 break;
1799 fallthrough; /* maybe userspace knows this conn_id */
1800 case HVCALL_POST_MESSAGE:
1801 /* don't bother userspace if it has no way to handle it */
1802 if (unlikely(rep || !vcpu_to_synic(vcpu)->active)) {
1803 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1804 break;
1805 }
1806 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
1807 vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
1808 vcpu->run->hyperv.u.hcall.input = param;
1809 vcpu->run->hyperv.u.hcall.params[0] = ingpa;
1810 vcpu->run->hyperv.u.hcall.params[1] = outgpa;
1811 vcpu->arch.complete_userspace_io =
1812 kvm_hv_hypercall_complete_userspace;
1813 return 0;
1814 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST:
1815 if (unlikely(fast || !rep_cnt || rep_idx)) {
1816 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1817 break;
1818 }
1819 ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, false);
1820 break;
1821 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE:
1822 if (unlikely(fast || rep)) {
1823 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1824 break;
1825 }
1826 ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, false);
1827 break;
1828 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX:
1829 if (unlikely(fast || !rep_cnt || rep_idx)) {
1830 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1831 break;
1832 }
1833 ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, true);
1834 break;
1835 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX:
1836 if (unlikely(fast || rep)) {
1837 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1838 break;
1839 }
1840 ret = kvm_hv_flush_tlb(vcpu, ingpa, rep_cnt, true);
1841 break;
1842 case HVCALL_SEND_IPI:
1843 if (unlikely(rep)) {
1844 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1845 break;
1846 }
1847 ret = kvm_hv_send_ipi(vcpu, ingpa, outgpa, false, fast);
1848 break;
1849 case HVCALL_SEND_IPI_EX:
1850 if (unlikely(fast || rep)) {
1851 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
1852 break;
1853 }
1854 ret = kvm_hv_send_ipi(vcpu, ingpa, outgpa, true, false);
1855 break;
1856 case HVCALL_POST_DEBUG_DATA:
1857 case HVCALL_RETRIEVE_DEBUG_DATA:
1858 if (unlikely(fast)) {
1859 ret = HV_STATUS_INVALID_PARAMETER;
1860 break;
1861 }
1862 fallthrough;
1863 case HVCALL_RESET_DEBUG_SESSION: {
1864 struct kvm_hv_syndbg *syndbg = vcpu_to_hv_syndbg(vcpu);
1865
1866 if (!kvm_hv_is_syndbg_enabled(vcpu)) {
1867 ret = HV_STATUS_INVALID_HYPERCALL_CODE;
1868 break;
1869 }
1870
1871 if (!(syndbg->options & HV_X64_SYNDBG_OPTION_USE_HCALLS)) {
1872 ret = HV_STATUS_OPERATION_DENIED;
1873 break;
1874 }
1875 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
1876 vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
1877 vcpu->run->hyperv.u.hcall.input = param;
1878 vcpu->run->hyperv.u.hcall.params[0] = ingpa;
1879 vcpu->run->hyperv.u.hcall.params[1] = outgpa;
1880 vcpu->arch.complete_userspace_io =
1881 kvm_hv_hypercall_complete_userspace;
1882 return 0;
1883 }
1884 default:
1885 ret = HV_STATUS_INVALID_HYPERCALL_CODE;
1886 break;
1887 }
1888
1889 return kvm_hv_hypercall_complete(vcpu, ret);
1890 }
1891
kvm_hv_init_vm(struct kvm * kvm)1892 void kvm_hv_init_vm(struct kvm *kvm)
1893 {
1894 mutex_init(&kvm->arch.hyperv.hv_lock);
1895 idr_init(&kvm->arch.hyperv.conn_to_evt);
1896 }
1897
kvm_hv_destroy_vm(struct kvm * kvm)1898 void kvm_hv_destroy_vm(struct kvm *kvm)
1899 {
1900 struct eventfd_ctx *eventfd;
1901 int i;
1902
1903 idr_for_each_entry(&kvm->arch.hyperv.conn_to_evt, eventfd, i)
1904 eventfd_ctx_put(eventfd);
1905 idr_destroy(&kvm->arch.hyperv.conn_to_evt);
1906 }
1907
kvm_hv_eventfd_assign(struct kvm * kvm,u32 conn_id,int fd)1908 static int kvm_hv_eventfd_assign(struct kvm *kvm, u32 conn_id, int fd)
1909 {
1910 struct kvm_hv *hv = &kvm->arch.hyperv;
1911 struct eventfd_ctx *eventfd;
1912 int ret;
1913
1914 eventfd = eventfd_ctx_fdget(fd);
1915 if (IS_ERR(eventfd))
1916 return PTR_ERR(eventfd);
1917
1918 mutex_lock(&hv->hv_lock);
1919 ret = idr_alloc(&hv->conn_to_evt, eventfd, conn_id, conn_id + 1,
1920 GFP_KERNEL_ACCOUNT);
1921 mutex_unlock(&hv->hv_lock);
1922
1923 if (ret >= 0)
1924 return 0;
1925
1926 if (ret == -ENOSPC)
1927 ret = -EEXIST;
1928 eventfd_ctx_put(eventfd);
1929 return ret;
1930 }
1931
kvm_hv_eventfd_deassign(struct kvm * kvm,u32 conn_id)1932 static int kvm_hv_eventfd_deassign(struct kvm *kvm, u32 conn_id)
1933 {
1934 struct kvm_hv *hv = &kvm->arch.hyperv;
1935 struct eventfd_ctx *eventfd;
1936
1937 mutex_lock(&hv->hv_lock);
1938 eventfd = idr_remove(&hv->conn_to_evt, conn_id);
1939 mutex_unlock(&hv->hv_lock);
1940
1941 if (!eventfd)
1942 return -ENOENT;
1943
1944 synchronize_srcu(&kvm->srcu);
1945 eventfd_ctx_put(eventfd);
1946 return 0;
1947 }
1948
kvm_vm_ioctl_hv_eventfd(struct kvm * kvm,struct kvm_hyperv_eventfd * args)1949 int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args)
1950 {
1951 if ((args->flags & ~KVM_HYPERV_EVENTFD_DEASSIGN) ||
1952 (args->conn_id & ~KVM_HYPERV_CONN_ID_MASK))
1953 return -EINVAL;
1954
1955 if (args->flags == KVM_HYPERV_EVENTFD_DEASSIGN)
1956 return kvm_hv_eventfd_deassign(kvm, args->conn_id);
1957 return kvm_hv_eventfd_assign(kvm, args->conn_id, args->fd);
1958 }
1959
kvm_vcpu_ioctl_get_hv_cpuid(struct kvm_vcpu * vcpu,struct kvm_cpuid2 * cpuid,struct kvm_cpuid_entry2 __user * entries)1960 int kvm_vcpu_ioctl_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,
1961 struct kvm_cpuid_entry2 __user *entries)
1962 {
1963 uint16_t evmcs_ver = 0;
1964 struct kvm_cpuid_entry2 cpuid_entries[] = {
1965 { .function = HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS },
1966 { .function = HYPERV_CPUID_INTERFACE },
1967 { .function = HYPERV_CPUID_VERSION },
1968 { .function = HYPERV_CPUID_FEATURES },
1969 { .function = HYPERV_CPUID_ENLIGHTMENT_INFO },
1970 { .function = HYPERV_CPUID_IMPLEMENT_LIMITS },
1971 { .function = HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS },
1972 { .function = HYPERV_CPUID_SYNDBG_INTERFACE },
1973 { .function = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES },
1974 { .function = HYPERV_CPUID_NESTED_FEATURES },
1975 };
1976 int i, nent = ARRAY_SIZE(cpuid_entries);
1977
1978 if (kvm_x86_ops.nested_ops->get_evmcs_version)
1979 evmcs_ver = kvm_x86_ops.nested_ops->get_evmcs_version(vcpu);
1980
1981 /* Skip NESTED_FEATURES if eVMCS is not supported */
1982 if (!evmcs_ver)
1983 --nent;
1984
1985 if (cpuid->nent < nent)
1986 return -E2BIG;
1987
1988 if (cpuid->nent > nent)
1989 cpuid->nent = nent;
1990
1991 for (i = 0; i < nent; i++) {
1992 struct kvm_cpuid_entry2 *ent = &cpuid_entries[i];
1993 u32 signature[3];
1994
1995 switch (ent->function) {
1996 case HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS:
1997 memcpy(signature, "Linux KVM Hv", 12);
1998
1999 ent->eax = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES;
2000 ent->ebx = signature[0];
2001 ent->ecx = signature[1];
2002 ent->edx = signature[2];
2003 break;
2004
2005 case HYPERV_CPUID_INTERFACE:
2006 memcpy(signature, "Hv#1\0\0\0\0\0\0\0\0", 12);
2007 ent->eax = signature[0];
2008 break;
2009
2010 case HYPERV_CPUID_VERSION:
2011 /*
2012 * We implement some Hyper-V 2016 functions so let's use
2013 * this version.
2014 */
2015 ent->eax = 0x00003839;
2016 ent->ebx = 0x000A0000;
2017 break;
2018
2019 case HYPERV_CPUID_FEATURES:
2020 ent->eax |= HV_MSR_VP_RUNTIME_AVAILABLE;
2021 ent->eax |= HV_MSR_TIME_REF_COUNT_AVAILABLE;
2022 ent->eax |= HV_MSR_SYNIC_AVAILABLE;
2023 ent->eax |= HV_MSR_SYNTIMER_AVAILABLE;
2024 ent->eax |= HV_MSR_APIC_ACCESS_AVAILABLE;
2025 ent->eax |= HV_MSR_HYPERCALL_AVAILABLE;
2026 ent->eax |= HV_MSR_VP_INDEX_AVAILABLE;
2027 ent->eax |= HV_MSR_RESET_AVAILABLE;
2028 ent->eax |= HV_MSR_REFERENCE_TSC_AVAILABLE;
2029 ent->eax |= HV_ACCESS_FREQUENCY_MSRS;
2030 ent->eax |= HV_ACCESS_REENLIGHTENMENT;
2031
2032 ent->ebx |= HV_POST_MESSAGES;
2033 ent->ebx |= HV_SIGNAL_EVENTS;
2034
2035 ent->edx |= HV_FEATURE_FREQUENCY_MSRS_AVAILABLE;
2036 ent->edx |= HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE;
2037
2038 ent->ebx |= HV_DEBUGGING;
2039 ent->edx |= HV_X64_GUEST_DEBUGGING_AVAILABLE;
2040 ent->edx |= HV_FEATURE_DEBUG_MSRS_AVAILABLE;
2041
2042 /*
2043 * Direct Synthetic timers only make sense with in-kernel
2044 * LAPIC
2045 */
2046 if (lapic_in_kernel(vcpu))
2047 ent->edx |= HV_STIMER_DIRECT_MODE_AVAILABLE;
2048
2049 break;
2050
2051 case HYPERV_CPUID_ENLIGHTMENT_INFO:
2052 ent->eax |= HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED;
2053 ent->eax |= HV_X64_APIC_ACCESS_RECOMMENDED;
2054 ent->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED;
2055 ent->eax |= HV_X64_CLUSTER_IPI_RECOMMENDED;
2056 ent->eax |= HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED;
2057 if (evmcs_ver)
2058 ent->eax |= HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
2059 if (!cpu_smt_possible())
2060 ent->eax |= HV_X64_NO_NONARCH_CORESHARING;
2061 /*
2062 * Default number of spinlock retry attempts, matches
2063 * HyperV 2016.
2064 */
2065 ent->ebx = 0x00000FFF;
2066
2067 break;
2068
2069 case HYPERV_CPUID_IMPLEMENT_LIMITS:
2070 /* Maximum number of virtual processors */
2071 ent->eax = KVM_MAX_VCPUS;
2072 /*
2073 * Maximum number of logical processors, matches
2074 * HyperV 2016.
2075 */
2076 ent->ebx = 64;
2077
2078 break;
2079
2080 case HYPERV_CPUID_NESTED_FEATURES:
2081 ent->eax = evmcs_ver;
2082
2083 break;
2084
2085 case HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS:
2086 memcpy(signature, "Linux KVM Hv", 12);
2087
2088 ent->eax = 0;
2089 ent->ebx = signature[0];
2090 ent->ecx = signature[1];
2091 ent->edx = signature[2];
2092 break;
2093
2094 case HYPERV_CPUID_SYNDBG_INTERFACE:
2095 memcpy(signature, "VS#1\0\0\0\0\0\0\0\0", 12);
2096 ent->eax = signature[0];
2097 break;
2098
2099 case HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES:
2100 ent->eax |= HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING;
2101 break;
2102
2103 default:
2104 break;
2105 }
2106 }
2107
2108 if (copy_to_user(entries, cpuid_entries,
2109 nent * sizeof(struct kvm_cpuid_entry2)))
2110 return -EFAULT;
2111
2112 return 0;
2113 }
2114