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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22 #include "x86.h"
23 #include "lapic.h"
24 #include "ioapic.h"
25 #include "cpuid.h"
26 #include "hyperv.h"
27 #include "mmu.h"
28 #include "xen.h"
29
30 #include <linux/cpu.h>
31 #include <linux/kvm_host.h>
32 #include <linux/highmem.h>
33 #include <linux/sched/cputime.h>
34 #include <linux/spinlock.h>
35 #include <linux/eventfd.h>
36
37 #include <asm/apicdef.h>
38 #include <asm/mshyperv.h>
39 #include <trace/events/kvm.h>
40
41 #include "trace.h"
42 #include "irq.h"
43 #include "fpu.h"
44
45 #define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, HV_VCPUS_PER_SPARSE_BANK)
46
47 /*
48 * As per Hyper-V TLFS, extended hypercalls start from 0x8001
49 * (HvExtCallQueryCapabilities). Response of this hypercalls is a 64 bit value
50 * where each bit tells which extended hypercall is available besides
51 * HvExtCallQueryCapabilities.
52 *
53 * 0x8001 - First extended hypercall, HvExtCallQueryCapabilities, no bit
54 * assigned.
55 *
56 * 0x8002 - Bit 0
57 * 0x8003 - Bit 1
58 * ..
59 * 0x8041 - Bit 63
60 *
61 * Therefore, HV_EXT_CALL_MAX = 0x8001 + 64
62 */
63 #define HV_EXT_CALL_MAX (HV_EXT_CALL_QUERY_CAPABILITIES + 64)
64
65 static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
66 bool vcpu_kick);
67
synic_read_sint(struct kvm_vcpu_hv_synic * synic,int sint)68 static inline u64 synic_read_sint(struct kvm_vcpu_hv_synic *synic, int sint)
69 {
70 return atomic64_read(&synic->sint[sint]);
71 }
72
synic_get_sint_vector(u64 sint_value)73 static inline int synic_get_sint_vector(u64 sint_value)
74 {
75 if (sint_value & HV_SYNIC_SINT_MASKED)
76 return -1;
77 return sint_value & HV_SYNIC_SINT_VECTOR_MASK;
78 }
79
synic_has_vector_connected(struct kvm_vcpu_hv_synic * synic,int vector)80 static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic *synic,
81 int vector)
82 {
83 int i;
84
85 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
86 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
87 return true;
88 }
89 return false;
90 }
91
synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic * synic,int vector)92 static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic *synic,
93 int vector)
94 {
95 int i;
96 u64 sint_value;
97
98 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
99 sint_value = synic_read_sint(synic, i);
100 if (synic_get_sint_vector(sint_value) == vector &&
101 sint_value & HV_SYNIC_SINT_AUTO_EOI)
102 return true;
103 }
104 return false;
105 }
106
synic_update_vector(struct kvm_vcpu_hv_synic * synic,int vector)107 static void synic_update_vector(struct kvm_vcpu_hv_synic *synic,
108 int vector)
109 {
110 struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
111 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
112 bool auto_eoi_old, auto_eoi_new;
113
114 if (vector < HV_SYNIC_FIRST_VALID_VECTOR)
115 return;
116
117 if (synic_has_vector_connected(synic, vector))
118 __set_bit(vector, synic->vec_bitmap);
119 else
120 __clear_bit(vector, synic->vec_bitmap);
121
122 auto_eoi_old = !bitmap_empty(synic->auto_eoi_bitmap, 256);
123
124 if (synic_has_vector_auto_eoi(synic, vector))
125 __set_bit(vector, synic->auto_eoi_bitmap);
126 else
127 __clear_bit(vector, synic->auto_eoi_bitmap);
128
129 auto_eoi_new = !bitmap_empty(synic->auto_eoi_bitmap, 256);
130
131 if (auto_eoi_old == auto_eoi_new)
132 return;
133
134 if (!enable_apicv)
135 return;
136
137 down_write(&vcpu->kvm->arch.apicv_update_lock);
138
139 if (auto_eoi_new)
140 hv->synic_auto_eoi_used++;
141 else
142 hv->synic_auto_eoi_used--;
143
144 /*
145 * Inhibit APICv if any vCPU is using SynIC's AutoEOI, which relies on
146 * the hypervisor to manually inject IRQs.
147 */
148 __kvm_set_or_clear_apicv_inhibit(vcpu->kvm,
149 APICV_INHIBIT_REASON_HYPERV,
150 !!hv->synic_auto_eoi_used);
151
152 up_write(&vcpu->kvm->arch.apicv_update_lock);
153 }
154
synic_set_sint(struct kvm_vcpu_hv_synic * synic,int sint,u64 data,bool host)155 static int synic_set_sint(struct kvm_vcpu_hv_synic *synic, int sint,
156 u64 data, bool host)
157 {
158 int vector, old_vector;
159 bool masked;
160
161 vector = data & HV_SYNIC_SINT_VECTOR_MASK;
162 masked = data & HV_SYNIC_SINT_MASKED;
163
164 /*
165 * Valid vectors are 16-255, however, nested Hyper-V attempts to write
166 * default '0x10000' value on boot and this should not #GP. We need to
167 * allow zero-initing the register from host as well.
168 */
169 if (vector < HV_SYNIC_FIRST_VALID_VECTOR && !host && !masked)
170 return 1;
171 /*
172 * Guest may configure multiple SINTs to use the same vector, so
173 * we maintain a bitmap of vectors handled by synic, and a
174 * bitmap of vectors with auto-eoi behavior. The bitmaps are
175 * updated here, and atomically queried on fast paths.
176 */
177 old_vector = synic_read_sint(synic, sint) & HV_SYNIC_SINT_VECTOR_MASK;
178
179 atomic64_set(&synic->sint[sint], data);
180
181 synic_update_vector(synic, old_vector);
182
183 synic_update_vector(synic, vector);
184
185 /* Load SynIC vectors into EOI exit bitmap */
186 kvm_make_request(KVM_REQ_SCAN_IOAPIC, hv_synic_to_vcpu(synic));
187 return 0;
188 }
189
get_vcpu_by_vpidx(struct kvm * kvm,u32 vpidx)190 static struct kvm_vcpu *get_vcpu_by_vpidx(struct kvm *kvm, u32 vpidx)
191 {
192 struct kvm_vcpu *vcpu = NULL;
193 unsigned long i;
194
195 if (vpidx >= KVM_MAX_VCPUS)
196 return NULL;
197
198 vcpu = kvm_get_vcpu(kvm, vpidx);
199 if (vcpu && kvm_hv_get_vpindex(vcpu) == vpidx)
200 return vcpu;
201 kvm_for_each_vcpu(i, vcpu, kvm)
202 if (kvm_hv_get_vpindex(vcpu) == vpidx)
203 return vcpu;
204 return NULL;
205 }
206
synic_get(struct kvm * kvm,u32 vpidx)207 static struct kvm_vcpu_hv_synic *synic_get(struct kvm *kvm, u32 vpidx)
208 {
209 struct kvm_vcpu *vcpu;
210 struct kvm_vcpu_hv_synic *synic;
211
212 vcpu = get_vcpu_by_vpidx(kvm, vpidx);
213 if (!vcpu || !to_hv_vcpu(vcpu))
214 return NULL;
215 synic = to_hv_synic(vcpu);
216 return (synic->active) ? synic : NULL;
217 }
218
kvm_hv_notify_acked_sint(struct kvm_vcpu * vcpu,u32 sint)219 static void kvm_hv_notify_acked_sint(struct kvm_vcpu *vcpu, u32 sint)
220 {
221 struct kvm *kvm = vcpu->kvm;
222 struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu);
223 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
224 struct kvm_vcpu_hv_stimer *stimer;
225 int gsi, idx;
226
227 trace_kvm_hv_notify_acked_sint(vcpu->vcpu_id, sint);
228
229 /* Try to deliver pending Hyper-V SynIC timers messages */
230 for (idx = 0; idx < ARRAY_SIZE(hv_vcpu->stimer); idx++) {
231 stimer = &hv_vcpu->stimer[idx];
232 if (stimer->msg_pending && stimer->config.enable &&
233 !stimer->config.direct_mode &&
234 stimer->config.sintx == sint)
235 stimer_mark_pending(stimer, false);
236 }
237
238 idx = srcu_read_lock(&kvm->irq_srcu);
239 gsi = atomic_read(&synic->sint_to_gsi[sint]);
240 if (gsi != -1)
241 kvm_notify_acked_gsi(kvm, gsi);
242 srcu_read_unlock(&kvm->irq_srcu, idx);
243 }
244
synic_exit(struct kvm_vcpu_hv_synic * synic,u32 msr)245 static void synic_exit(struct kvm_vcpu_hv_synic *synic, u32 msr)
246 {
247 struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
248 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
249
250 hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNIC;
251 hv_vcpu->exit.u.synic.msr = msr;
252 hv_vcpu->exit.u.synic.control = synic->control;
253 hv_vcpu->exit.u.synic.evt_page = synic->evt_page;
254 hv_vcpu->exit.u.synic.msg_page = synic->msg_page;
255
256 kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
257 }
258
synic_set_msr(struct kvm_vcpu_hv_synic * synic,u32 msr,u64 data,bool host)259 static int synic_set_msr(struct kvm_vcpu_hv_synic *synic,
260 u32 msr, u64 data, bool host)
261 {
262 struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
263 int ret;
264
265 if (!synic->active && (!host || data))
266 return 1;
267
268 trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host);
269
270 ret = 0;
271 switch (msr) {
272 case HV_X64_MSR_SCONTROL:
273 synic->control = data;
274 if (!host)
275 synic_exit(synic, msr);
276 break;
277 case HV_X64_MSR_SVERSION:
278 if (!host) {
279 ret = 1;
280 break;
281 }
282 synic->version = data;
283 break;
284 case HV_X64_MSR_SIEFP:
285 if ((data & HV_SYNIC_SIEFP_ENABLE) && !host &&
286 !synic->dont_zero_synic_pages)
287 if (kvm_clear_guest(vcpu->kvm,
288 data & PAGE_MASK, PAGE_SIZE)) {
289 ret = 1;
290 break;
291 }
292 synic->evt_page = data;
293 if (!host)
294 synic_exit(synic, msr);
295 break;
296 case HV_X64_MSR_SIMP:
297 if ((data & HV_SYNIC_SIMP_ENABLE) && !host &&
298 !synic->dont_zero_synic_pages)
299 if (kvm_clear_guest(vcpu->kvm,
300 data & PAGE_MASK, PAGE_SIZE)) {
301 ret = 1;
302 break;
303 }
304 synic->msg_page = data;
305 if (!host)
306 synic_exit(synic, msr);
307 break;
308 case HV_X64_MSR_EOM: {
309 int i;
310
311 if (!synic->active)
312 break;
313
314 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
315 kvm_hv_notify_acked_sint(vcpu, i);
316 break;
317 }
318 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
319 ret = synic_set_sint(synic, msr - HV_X64_MSR_SINT0, data, host);
320 break;
321 default:
322 ret = 1;
323 break;
324 }
325 return ret;
326 }
327
kvm_hv_is_syndbg_enabled(struct kvm_vcpu * vcpu)328 static bool kvm_hv_is_syndbg_enabled(struct kvm_vcpu *vcpu)
329 {
330 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
331
332 return hv_vcpu->cpuid_cache.syndbg_cap_eax &
333 HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING;
334 }
335
kvm_hv_syndbg_complete_userspace(struct kvm_vcpu * vcpu)336 static int kvm_hv_syndbg_complete_userspace(struct kvm_vcpu *vcpu)
337 {
338 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
339
340 if (vcpu->run->hyperv.u.syndbg.msr == HV_X64_MSR_SYNDBG_CONTROL)
341 hv->hv_syndbg.control.status =
342 vcpu->run->hyperv.u.syndbg.status;
343 return 1;
344 }
345
syndbg_exit(struct kvm_vcpu * vcpu,u32 msr)346 static void syndbg_exit(struct kvm_vcpu *vcpu, u32 msr)
347 {
348 struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu);
349 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
350
351 hv_vcpu->exit.type = KVM_EXIT_HYPERV_SYNDBG;
352 hv_vcpu->exit.u.syndbg.msr = msr;
353 hv_vcpu->exit.u.syndbg.control = syndbg->control.control;
354 hv_vcpu->exit.u.syndbg.send_page = syndbg->control.send_page;
355 hv_vcpu->exit.u.syndbg.recv_page = syndbg->control.recv_page;
356 hv_vcpu->exit.u.syndbg.pending_page = syndbg->control.pending_page;
357 vcpu->arch.complete_userspace_io =
358 kvm_hv_syndbg_complete_userspace;
359
360 kvm_make_request(KVM_REQ_HV_EXIT, vcpu);
361 }
362
syndbg_set_msr(struct kvm_vcpu * vcpu,u32 msr,u64 data,bool host)363 static int syndbg_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
364 {
365 struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu);
366
367 if (!kvm_hv_is_syndbg_enabled(vcpu) && !host)
368 return 1;
369
370 trace_kvm_hv_syndbg_set_msr(vcpu->vcpu_id,
371 to_hv_vcpu(vcpu)->vp_index, msr, data);
372 switch (msr) {
373 case HV_X64_MSR_SYNDBG_CONTROL:
374 syndbg->control.control = data;
375 if (!host)
376 syndbg_exit(vcpu, msr);
377 break;
378 case HV_X64_MSR_SYNDBG_STATUS:
379 syndbg->control.status = data;
380 break;
381 case HV_X64_MSR_SYNDBG_SEND_BUFFER:
382 syndbg->control.send_page = data;
383 break;
384 case HV_X64_MSR_SYNDBG_RECV_BUFFER:
385 syndbg->control.recv_page = data;
386 break;
387 case HV_X64_MSR_SYNDBG_PENDING_BUFFER:
388 syndbg->control.pending_page = data;
389 if (!host)
390 syndbg_exit(vcpu, msr);
391 break;
392 case HV_X64_MSR_SYNDBG_OPTIONS:
393 syndbg->options = data;
394 break;
395 default:
396 break;
397 }
398
399 return 0;
400 }
401
syndbg_get_msr(struct kvm_vcpu * vcpu,u32 msr,u64 * pdata,bool host)402 static int syndbg_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
403 {
404 struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu);
405
406 if (!kvm_hv_is_syndbg_enabled(vcpu) && !host)
407 return 1;
408
409 switch (msr) {
410 case HV_X64_MSR_SYNDBG_CONTROL:
411 *pdata = syndbg->control.control;
412 break;
413 case HV_X64_MSR_SYNDBG_STATUS:
414 *pdata = syndbg->control.status;
415 break;
416 case HV_X64_MSR_SYNDBG_SEND_BUFFER:
417 *pdata = syndbg->control.send_page;
418 break;
419 case HV_X64_MSR_SYNDBG_RECV_BUFFER:
420 *pdata = syndbg->control.recv_page;
421 break;
422 case HV_X64_MSR_SYNDBG_PENDING_BUFFER:
423 *pdata = syndbg->control.pending_page;
424 break;
425 case HV_X64_MSR_SYNDBG_OPTIONS:
426 *pdata = syndbg->options;
427 break;
428 default:
429 break;
430 }
431
432 trace_kvm_hv_syndbg_get_msr(vcpu->vcpu_id, kvm_hv_get_vpindex(vcpu), msr, *pdata);
433
434 return 0;
435 }
436
synic_get_msr(struct kvm_vcpu_hv_synic * synic,u32 msr,u64 * pdata,bool host)437 static int synic_get_msr(struct kvm_vcpu_hv_synic *synic, u32 msr, u64 *pdata,
438 bool host)
439 {
440 int ret;
441
442 if (!synic->active && !host)
443 return 1;
444
445 ret = 0;
446 switch (msr) {
447 case HV_X64_MSR_SCONTROL:
448 *pdata = synic->control;
449 break;
450 case HV_X64_MSR_SVERSION:
451 *pdata = synic->version;
452 break;
453 case HV_X64_MSR_SIEFP:
454 *pdata = synic->evt_page;
455 break;
456 case HV_X64_MSR_SIMP:
457 *pdata = synic->msg_page;
458 break;
459 case HV_X64_MSR_EOM:
460 *pdata = 0;
461 break;
462 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
463 *pdata = atomic64_read(&synic->sint[msr - HV_X64_MSR_SINT0]);
464 break;
465 default:
466 ret = 1;
467 break;
468 }
469 return ret;
470 }
471
synic_set_irq(struct kvm_vcpu_hv_synic * synic,u32 sint)472 static int synic_set_irq(struct kvm_vcpu_hv_synic *synic, u32 sint)
473 {
474 struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
475 struct kvm_lapic_irq irq;
476 int ret, vector;
477
478 if (KVM_BUG_ON(!lapic_in_kernel(vcpu), vcpu->kvm))
479 return -EINVAL;
480
481 if (sint >= ARRAY_SIZE(synic->sint))
482 return -EINVAL;
483
484 vector = synic_get_sint_vector(synic_read_sint(synic, sint));
485 if (vector < 0)
486 return -ENOENT;
487
488 memset(&irq, 0, sizeof(irq));
489 irq.shorthand = APIC_DEST_SELF;
490 irq.dest_mode = APIC_DEST_PHYSICAL;
491 irq.delivery_mode = APIC_DM_FIXED;
492 irq.vector = vector;
493 irq.level = 1;
494
495 ret = kvm_irq_delivery_to_apic(vcpu->kvm, vcpu->arch.apic, &irq);
496 trace_kvm_hv_synic_set_irq(vcpu->vcpu_id, sint, irq.vector, ret);
497 return ret;
498 }
499
kvm_hv_synic_set_irq(struct kvm_kernel_irq_routing_entry * e,struct kvm * kvm,int irq_source_id,int level,bool line_status)500 int kvm_hv_synic_set_irq(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
501 int irq_source_id, int level, bool line_status)
502 {
503 struct kvm_vcpu_hv_synic *synic;
504
505 if (!level)
506 return -1;
507
508 synic = synic_get(kvm, e->hv_sint.vcpu);
509 if (!synic)
510 return -EINVAL;
511
512 return synic_set_irq(synic, e->hv_sint.sint);
513 }
514
kvm_hv_synic_send_eoi(struct kvm_vcpu * vcpu,int vector)515 void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
516 {
517 struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu);
518 int i;
519
520 trace_kvm_hv_synic_send_eoi(vcpu->vcpu_id, vector);
521
522 for (i = 0; i < ARRAY_SIZE(synic->sint); i++)
523 if (synic_get_sint_vector(synic_read_sint(synic, i)) == vector)
524 kvm_hv_notify_acked_sint(vcpu, i);
525 }
526
kvm_hv_set_sint_gsi(struct kvm * kvm,u32 vpidx,u32 sint,int gsi)527 static int kvm_hv_set_sint_gsi(struct kvm *kvm, u32 vpidx, u32 sint, int gsi)
528 {
529 struct kvm_vcpu_hv_synic *synic;
530
531 synic = synic_get(kvm, vpidx);
532 if (!synic)
533 return -EINVAL;
534
535 if (sint >= ARRAY_SIZE(synic->sint_to_gsi))
536 return -EINVAL;
537
538 atomic_set(&synic->sint_to_gsi[sint], gsi);
539 return 0;
540 }
541
kvm_hv_irq_routing_update(struct kvm * kvm)542 void kvm_hv_irq_routing_update(struct kvm *kvm)
543 {
544 struct kvm_irq_routing_table *irq_rt;
545 struct kvm_kernel_irq_routing_entry *e;
546 u32 gsi;
547
548 irq_rt = srcu_dereference_check(kvm->irq_routing, &kvm->irq_srcu,
549 lockdep_is_held(&kvm->irq_lock));
550
551 for (gsi = 0; gsi < irq_rt->nr_rt_entries; gsi++) {
552 hlist_for_each_entry(e, &irq_rt->map[gsi], link) {
553 if (e->type == KVM_IRQ_ROUTING_HV_SINT)
554 kvm_hv_set_sint_gsi(kvm, e->hv_sint.vcpu,
555 e->hv_sint.sint, gsi);
556 }
557 }
558 }
559
synic_init(struct kvm_vcpu_hv_synic * synic)560 static void synic_init(struct kvm_vcpu_hv_synic *synic)
561 {
562 int i;
563
564 memset(synic, 0, sizeof(*synic));
565 synic->version = HV_SYNIC_VERSION_1;
566 for (i = 0; i < ARRAY_SIZE(synic->sint); i++) {
567 atomic64_set(&synic->sint[i], HV_SYNIC_SINT_MASKED);
568 atomic_set(&synic->sint_to_gsi[i], -1);
569 }
570 }
571
get_time_ref_counter(struct kvm * kvm)572 static u64 get_time_ref_counter(struct kvm *kvm)
573 {
574 struct kvm_hv *hv = to_kvm_hv(kvm);
575 struct kvm_vcpu *vcpu;
576 u64 tsc;
577
578 /*
579 * Fall back to get_kvmclock_ns() when TSC page hasn't been set up,
580 * is broken, disabled or being updated.
581 */
582 if (hv->hv_tsc_page_status != HV_TSC_PAGE_SET)
583 return div_u64(get_kvmclock_ns(kvm), 100);
584
585 vcpu = kvm_get_vcpu(kvm, 0);
586 tsc = kvm_read_l1_tsc(vcpu, rdtsc());
587 return mul_u64_u64_shr(tsc, hv->tsc_ref.tsc_scale, 64)
588 + hv->tsc_ref.tsc_offset;
589 }
590
stimer_mark_pending(struct kvm_vcpu_hv_stimer * stimer,bool vcpu_kick)591 static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer,
592 bool vcpu_kick)
593 {
594 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
595
596 set_bit(stimer->index,
597 to_hv_vcpu(vcpu)->stimer_pending_bitmap);
598 kvm_make_request(KVM_REQ_HV_STIMER, vcpu);
599 if (vcpu_kick)
600 kvm_vcpu_kick(vcpu);
601 }
602
stimer_cleanup(struct kvm_vcpu_hv_stimer * stimer)603 static void stimer_cleanup(struct kvm_vcpu_hv_stimer *stimer)
604 {
605 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
606
607 trace_kvm_hv_stimer_cleanup(hv_stimer_to_vcpu(stimer)->vcpu_id,
608 stimer->index);
609
610 hrtimer_cancel(&stimer->timer);
611 clear_bit(stimer->index,
612 to_hv_vcpu(vcpu)->stimer_pending_bitmap);
613 stimer->msg_pending = false;
614 stimer->exp_time = 0;
615 }
616
stimer_timer_callback(struct hrtimer * timer)617 static enum hrtimer_restart stimer_timer_callback(struct hrtimer *timer)
618 {
619 struct kvm_vcpu_hv_stimer *stimer;
620
621 stimer = container_of(timer, struct kvm_vcpu_hv_stimer, timer);
622 trace_kvm_hv_stimer_callback(hv_stimer_to_vcpu(stimer)->vcpu_id,
623 stimer->index);
624 stimer_mark_pending(stimer, true);
625
626 return HRTIMER_NORESTART;
627 }
628
629 /*
630 * stimer_start() assumptions:
631 * a) stimer->count is not equal to 0
632 * b) stimer->config has HV_STIMER_ENABLE flag
633 */
stimer_start(struct kvm_vcpu_hv_stimer * stimer)634 static int stimer_start(struct kvm_vcpu_hv_stimer *stimer)
635 {
636 u64 time_now;
637 ktime_t ktime_now;
638
639 time_now = get_time_ref_counter(hv_stimer_to_vcpu(stimer)->kvm);
640 ktime_now = ktime_get();
641
642 if (stimer->config.periodic) {
643 if (stimer->exp_time) {
644 if (time_now >= stimer->exp_time) {
645 u64 remainder;
646
647 div64_u64_rem(time_now - stimer->exp_time,
648 stimer->count, &remainder);
649 stimer->exp_time =
650 time_now + (stimer->count - remainder);
651 }
652 } else
653 stimer->exp_time = time_now + stimer->count;
654
655 trace_kvm_hv_stimer_start_periodic(
656 hv_stimer_to_vcpu(stimer)->vcpu_id,
657 stimer->index,
658 time_now, stimer->exp_time);
659
660 hrtimer_start(&stimer->timer,
661 ktime_add_ns(ktime_now,
662 100 * (stimer->exp_time - time_now)),
663 HRTIMER_MODE_ABS);
664 return 0;
665 }
666 stimer->exp_time = stimer->count;
667 if (time_now >= stimer->count) {
668 /*
669 * Expire timer according to Hypervisor Top-Level Functional
670 * specification v4(15.3.1):
671 * "If a one shot is enabled and the specified count is in
672 * the past, it will expire immediately."
673 */
674 stimer_mark_pending(stimer, false);
675 return 0;
676 }
677
678 trace_kvm_hv_stimer_start_one_shot(hv_stimer_to_vcpu(stimer)->vcpu_id,
679 stimer->index,
680 time_now, stimer->count);
681
682 hrtimer_start(&stimer->timer,
683 ktime_add_ns(ktime_now, 100 * (stimer->count - time_now)),
684 HRTIMER_MODE_ABS);
685 return 0;
686 }
687
stimer_set_config(struct kvm_vcpu_hv_stimer * stimer,u64 config,bool host)688 static int stimer_set_config(struct kvm_vcpu_hv_stimer *stimer, u64 config,
689 bool host)
690 {
691 union hv_stimer_config new_config = {.as_uint64 = config},
692 old_config = {.as_uint64 = stimer->config.as_uint64};
693 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
694 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
695 struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu);
696
697 if (!synic->active && (!host || config))
698 return 1;
699
700 if (unlikely(!host && hv_vcpu->enforce_cpuid && new_config.direct_mode &&
701 !(hv_vcpu->cpuid_cache.features_edx &
702 HV_STIMER_DIRECT_MODE_AVAILABLE)))
703 return 1;
704
705 trace_kvm_hv_stimer_set_config(hv_stimer_to_vcpu(stimer)->vcpu_id,
706 stimer->index, config, host);
707
708 stimer_cleanup(stimer);
709 if (old_config.enable &&
710 !new_config.direct_mode && new_config.sintx == 0)
711 new_config.enable = 0;
712 stimer->config.as_uint64 = new_config.as_uint64;
713
714 if (stimer->config.enable)
715 stimer_mark_pending(stimer, false);
716
717 return 0;
718 }
719
stimer_set_count(struct kvm_vcpu_hv_stimer * stimer,u64 count,bool host)720 static int stimer_set_count(struct kvm_vcpu_hv_stimer *stimer, u64 count,
721 bool host)
722 {
723 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
724 struct kvm_vcpu_hv_synic *synic = to_hv_synic(vcpu);
725
726 if (!synic->active && (!host || count))
727 return 1;
728
729 trace_kvm_hv_stimer_set_count(hv_stimer_to_vcpu(stimer)->vcpu_id,
730 stimer->index, count, host);
731
732 stimer_cleanup(stimer);
733 stimer->count = count;
734 if (!host) {
735 if (stimer->count == 0)
736 stimer->config.enable = 0;
737 else if (stimer->config.auto_enable)
738 stimer->config.enable = 1;
739 }
740
741 if (stimer->config.enable)
742 stimer_mark_pending(stimer, false);
743
744 return 0;
745 }
746
stimer_get_config(struct kvm_vcpu_hv_stimer * stimer,u64 * pconfig)747 static int stimer_get_config(struct kvm_vcpu_hv_stimer *stimer, u64 *pconfig)
748 {
749 *pconfig = stimer->config.as_uint64;
750 return 0;
751 }
752
stimer_get_count(struct kvm_vcpu_hv_stimer * stimer,u64 * pcount)753 static int stimer_get_count(struct kvm_vcpu_hv_stimer *stimer, u64 *pcount)
754 {
755 *pcount = stimer->count;
756 return 0;
757 }
758
synic_deliver_msg(struct kvm_vcpu_hv_synic * synic,u32 sint,struct hv_message * src_msg,bool no_retry)759 static int synic_deliver_msg(struct kvm_vcpu_hv_synic *synic, u32 sint,
760 struct hv_message *src_msg, bool no_retry)
761 {
762 struct kvm_vcpu *vcpu = hv_synic_to_vcpu(synic);
763 int msg_off = offsetof(struct hv_message_page, sint_message[sint]);
764 gfn_t msg_page_gfn;
765 struct hv_message_header hv_hdr;
766 int r;
767
768 if (!(synic->msg_page & HV_SYNIC_SIMP_ENABLE))
769 return -ENOENT;
770
771 msg_page_gfn = synic->msg_page >> PAGE_SHIFT;
772
773 /*
774 * Strictly following the spec-mandated ordering would assume setting
775 * .msg_pending before checking .message_type. However, this function
776 * is only called in vcpu context so the entire update is atomic from
777 * guest POV and thus the exact order here doesn't matter.
778 */
779 r = kvm_vcpu_read_guest_page(vcpu, msg_page_gfn, &hv_hdr.message_type,
780 msg_off + offsetof(struct hv_message,
781 header.message_type),
782 sizeof(hv_hdr.message_type));
783 if (r < 0)
784 return r;
785
786 if (hv_hdr.message_type != HVMSG_NONE) {
787 if (no_retry)
788 return 0;
789
790 hv_hdr.message_flags.msg_pending = 1;
791 r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn,
792 &hv_hdr.message_flags,
793 msg_off +
794 offsetof(struct hv_message,
795 header.message_flags),
796 sizeof(hv_hdr.message_flags));
797 if (r < 0)
798 return r;
799 return -EAGAIN;
800 }
801
802 r = kvm_vcpu_write_guest_page(vcpu, msg_page_gfn, src_msg, msg_off,
803 sizeof(src_msg->header) +
804 src_msg->header.payload_size);
805 if (r < 0)
806 return r;
807
808 r = synic_set_irq(synic, sint);
809 if (r < 0)
810 return r;
811 if (r == 0)
812 return -EFAULT;
813 return 0;
814 }
815
stimer_send_msg(struct kvm_vcpu_hv_stimer * stimer)816 static int stimer_send_msg(struct kvm_vcpu_hv_stimer *stimer)
817 {
818 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
819 struct hv_message *msg = &stimer->msg;
820 struct hv_timer_message_payload *payload =
821 (struct hv_timer_message_payload *)&msg->u.payload;
822
823 /*
824 * To avoid piling up periodic ticks, don't retry message
825 * delivery for them (within "lazy" lost ticks policy).
826 */
827 bool no_retry = stimer->config.periodic;
828
829 payload->expiration_time = stimer->exp_time;
830 payload->delivery_time = get_time_ref_counter(vcpu->kvm);
831 return synic_deliver_msg(to_hv_synic(vcpu),
832 stimer->config.sintx, msg,
833 no_retry);
834 }
835
stimer_notify_direct(struct kvm_vcpu_hv_stimer * stimer)836 static int stimer_notify_direct(struct kvm_vcpu_hv_stimer *stimer)
837 {
838 struct kvm_vcpu *vcpu = hv_stimer_to_vcpu(stimer);
839 struct kvm_lapic_irq irq = {
840 .delivery_mode = APIC_DM_FIXED,
841 .vector = stimer->config.apic_vector
842 };
843
844 if (lapic_in_kernel(vcpu))
845 return !kvm_apic_set_irq(vcpu, &irq, NULL);
846 return 0;
847 }
848
stimer_expiration(struct kvm_vcpu_hv_stimer * stimer)849 static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer)
850 {
851 int r, direct = stimer->config.direct_mode;
852
853 stimer->msg_pending = true;
854 if (!direct)
855 r = stimer_send_msg(stimer);
856 else
857 r = stimer_notify_direct(stimer);
858 trace_kvm_hv_stimer_expiration(hv_stimer_to_vcpu(stimer)->vcpu_id,
859 stimer->index, direct, r);
860 if (!r) {
861 stimer->msg_pending = false;
862 if (!(stimer->config.periodic))
863 stimer->config.enable = 0;
864 }
865 }
866
kvm_hv_process_stimers(struct kvm_vcpu * vcpu)867 void kvm_hv_process_stimers(struct kvm_vcpu *vcpu)
868 {
869 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
870 struct kvm_vcpu_hv_stimer *stimer;
871 u64 time_now, exp_time;
872 int i;
873
874 if (!hv_vcpu)
875 return;
876
877 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
878 if (test_and_clear_bit(i, hv_vcpu->stimer_pending_bitmap)) {
879 stimer = &hv_vcpu->stimer[i];
880 if (stimer->config.enable) {
881 exp_time = stimer->exp_time;
882
883 if (exp_time) {
884 time_now =
885 get_time_ref_counter(vcpu->kvm);
886 if (time_now >= exp_time)
887 stimer_expiration(stimer);
888 }
889
890 if ((stimer->config.enable) &&
891 stimer->count) {
892 if (!stimer->msg_pending)
893 stimer_start(stimer);
894 } else
895 stimer_cleanup(stimer);
896 }
897 }
898 }
899
kvm_hv_vcpu_uninit(struct kvm_vcpu * vcpu)900 void kvm_hv_vcpu_uninit(struct kvm_vcpu *vcpu)
901 {
902 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
903 int i;
904
905 if (!hv_vcpu)
906 return;
907
908 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
909 stimer_cleanup(&hv_vcpu->stimer[i]);
910
911 kfree(hv_vcpu);
912 vcpu->arch.hyperv = NULL;
913 }
914
kvm_hv_assist_page_enabled(struct kvm_vcpu * vcpu)915 bool kvm_hv_assist_page_enabled(struct kvm_vcpu *vcpu)
916 {
917 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
918
919 if (!hv_vcpu)
920 return false;
921
922 if (!(hv_vcpu->hv_vapic & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE))
923 return false;
924 return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
925 }
926 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_hv_assist_page_enabled);
927
kvm_hv_get_assist_page(struct kvm_vcpu * vcpu)928 int kvm_hv_get_assist_page(struct kvm_vcpu *vcpu)
929 {
930 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
931
932 if (!hv_vcpu || !kvm_hv_assist_page_enabled(vcpu))
933 return -EFAULT;
934
935 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data,
936 &hv_vcpu->vp_assist_page, sizeof(struct hv_vp_assist_page));
937 }
938 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_hv_get_assist_page);
939
stimer_prepare_msg(struct kvm_vcpu_hv_stimer * stimer)940 static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer *stimer)
941 {
942 struct hv_message *msg = &stimer->msg;
943 struct hv_timer_message_payload *payload =
944 (struct hv_timer_message_payload *)&msg->u.payload;
945
946 memset(&msg->header, 0, sizeof(msg->header));
947 msg->header.message_type = HVMSG_TIMER_EXPIRED;
948 msg->header.payload_size = sizeof(*payload);
949
950 payload->timer_index = stimer->index;
951 payload->expiration_time = 0;
952 payload->delivery_time = 0;
953 }
954
stimer_init(struct kvm_vcpu_hv_stimer * stimer,int timer_index)955 static void stimer_init(struct kvm_vcpu_hv_stimer *stimer, int timer_index)
956 {
957 memset(stimer, 0, sizeof(*stimer));
958 stimer->index = timer_index;
959 hrtimer_setup(&stimer->timer, stimer_timer_callback, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
960 stimer_prepare_msg(stimer);
961 }
962
kvm_hv_vcpu_init(struct kvm_vcpu * vcpu)963 int kvm_hv_vcpu_init(struct kvm_vcpu *vcpu)
964 {
965 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
966 int i;
967
968 if (hv_vcpu)
969 return 0;
970
971 hv_vcpu = kzalloc_obj(struct kvm_vcpu_hv, GFP_KERNEL_ACCOUNT);
972 if (!hv_vcpu)
973 return -ENOMEM;
974
975 vcpu->arch.hyperv = hv_vcpu;
976 hv_vcpu->vcpu = vcpu;
977
978 synic_init(&hv_vcpu->synic);
979
980 bitmap_zero(hv_vcpu->stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
981 for (i = 0; i < ARRAY_SIZE(hv_vcpu->stimer); i++)
982 stimer_init(&hv_vcpu->stimer[i], i);
983
984 hv_vcpu->vp_index = vcpu->vcpu_idx;
985
986 for (i = 0; i < HV_NR_TLB_FLUSH_FIFOS; i++) {
987 INIT_KFIFO(hv_vcpu->tlb_flush_fifo[i].entries);
988 spin_lock_init(&hv_vcpu->tlb_flush_fifo[i].write_lock);
989 }
990
991 return 0;
992 }
993
kvm_hv_activate_synic(struct kvm_vcpu * vcpu,bool dont_zero_synic_pages)994 int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_pages)
995 {
996 struct kvm_vcpu_hv_synic *synic;
997 int r;
998
999 r = kvm_hv_vcpu_init(vcpu);
1000 if (r)
1001 return r;
1002
1003 synic = to_hv_synic(vcpu);
1004
1005 synic->active = true;
1006 synic->dont_zero_synic_pages = dont_zero_synic_pages;
1007 synic->control = HV_SYNIC_CONTROL_ENABLE;
1008 return 0;
1009 }
1010
kvm_hv_msr_partition_wide(u32 msr)1011 static bool kvm_hv_msr_partition_wide(u32 msr)
1012 {
1013 bool r = false;
1014
1015 switch (msr) {
1016 case HV_X64_MSR_GUEST_OS_ID:
1017 case HV_X64_MSR_HYPERCALL:
1018 case HV_X64_MSR_REFERENCE_TSC:
1019 case HV_X64_MSR_TIME_REF_COUNT:
1020 case HV_X64_MSR_CRASH_CTL:
1021 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1022 case HV_X64_MSR_RESET:
1023 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1024 case HV_X64_MSR_TSC_EMULATION_CONTROL:
1025 case HV_X64_MSR_TSC_EMULATION_STATUS:
1026 case HV_X64_MSR_TSC_INVARIANT_CONTROL:
1027 case HV_X64_MSR_SYNDBG_OPTIONS:
1028 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
1029 r = true;
1030 break;
1031 }
1032
1033 return r;
1034 }
1035
kvm_hv_msr_get_crash_data(struct kvm * kvm,u32 index,u64 * pdata)1036 static int kvm_hv_msr_get_crash_data(struct kvm *kvm, u32 index, u64 *pdata)
1037 {
1038 struct kvm_hv *hv = to_kvm_hv(kvm);
1039 size_t size = ARRAY_SIZE(hv->hv_crash_param);
1040
1041 if (WARN_ON_ONCE(index >= size))
1042 return -EINVAL;
1043
1044 *pdata = hv->hv_crash_param[array_index_nospec(index, size)];
1045 return 0;
1046 }
1047
kvm_hv_msr_get_crash_ctl(struct kvm * kvm,u64 * pdata)1048 static int kvm_hv_msr_get_crash_ctl(struct kvm *kvm, u64 *pdata)
1049 {
1050 struct kvm_hv *hv = to_kvm_hv(kvm);
1051
1052 *pdata = hv->hv_crash_ctl;
1053 return 0;
1054 }
1055
kvm_hv_msr_set_crash_ctl(struct kvm * kvm,u64 data)1056 static int kvm_hv_msr_set_crash_ctl(struct kvm *kvm, u64 data)
1057 {
1058 struct kvm_hv *hv = to_kvm_hv(kvm);
1059
1060 hv->hv_crash_ctl = data & HV_CRASH_CTL_CRASH_NOTIFY;
1061
1062 return 0;
1063 }
1064
kvm_hv_msr_set_crash_data(struct kvm * kvm,u32 index,u64 data)1065 static int kvm_hv_msr_set_crash_data(struct kvm *kvm, u32 index, u64 data)
1066 {
1067 struct kvm_hv *hv = to_kvm_hv(kvm);
1068 size_t size = ARRAY_SIZE(hv->hv_crash_param);
1069
1070 if (WARN_ON_ONCE(index >= size))
1071 return -EINVAL;
1072
1073 hv->hv_crash_param[array_index_nospec(index, size)] = data;
1074 return 0;
1075 }
1076
1077 /*
1078 * The kvmclock and Hyper-V TSC page use similar formulas, and converting
1079 * between them is possible:
1080 *
1081 * kvmclock formula:
1082 * nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32)
1083 * + system_time
1084 *
1085 * Hyper-V formula:
1086 * nsec/100 = ticks * scale / 2^64 + offset
1087 *
1088 * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula.
1089 * By dividing the kvmclock formula by 100 and equating what's left we get:
1090 * ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
1091 * scale / 2^64 = tsc_to_system_mul * 2^(tsc_shift-32) / 100
1092 * scale = tsc_to_system_mul * 2^(32+tsc_shift) / 100
1093 *
1094 * Now expand the kvmclock formula and divide by 100:
1095 * nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32)
1096 * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32)
1097 * + system_time
1098 * nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
1099 * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100
1100 * + system_time / 100
1101 *
1102 * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64:
1103 * nsec/100 = ticks * scale / 2^64
1104 * - tsc_timestamp * scale / 2^64
1105 * + system_time / 100
1106 *
1107 * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out:
1108 * offset = system_time / 100 - tsc_timestamp * scale / 2^64
1109 *
1110 * These two equivalencies are implemented in this function.
1111 */
compute_tsc_page_parameters(struct pvclock_vcpu_time_info * hv_clock,struct ms_hyperv_tsc_page * tsc_ref)1112 static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info *hv_clock,
1113 struct ms_hyperv_tsc_page *tsc_ref)
1114 {
1115 u64 max_mul;
1116
1117 if (!(hv_clock->flags & PVCLOCK_TSC_STABLE_BIT))
1118 return false;
1119
1120 /*
1121 * check if scale would overflow, if so we use the time ref counter
1122 * tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64
1123 * tsc_to_system_mul / 100 >= 2^(32-tsc_shift)
1124 * tsc_to_system_mul >= 100 * 2^(32-tsc_shift)
1125 */
1126 max_mul = 100ull << (32 - hv_clock->tsc_shift);
1127 if (hv_clock->tsc_to_system_mul >= max_mul)
1128 return false;
1129
1130 /*
1131 * Otherwise compute the scale and offset according to the formulas
1132 * derived above.
1133 */
1134 tsc_ref->tsc_scale =
1135 mul_u64_u32_div(1ULL << (32 + hv_clock->tsc_shift),
1136 hv_clock->tsc_to_system_mul,
1137 100);
1138
1139 tsc_ref->tsc_offset = hv_clock->system_time;
1140 do_div(tsc_ref->tsc_offset, 100);
1141 tsc_ref->tsc_offset -=
1142 mul_u64_u64_shr(hv_clock->tsc_timestamp, tsc_ref->tsc_scale, 64);
1143 return true;
1144 }
1145
1146 /*
1147 * Don't touch TSC page values if the guest has opted for TSC emulation after
1148 * migration. KVM doesn't fully support reenlightenment notifications and TSC
1149 * access emulation and Hyper-V is known to expect the values in TSC page to
1150 * stay constant before TSC access emulation is disabled from guest side
1151 * (HV_X64_MSR_TSC_EMULATION_STATUS). KVM userspace is expected to preserve TSC
1152 * frequency and guest visible TSC value across migration (and prevent it when
1153 * TSC scaling is unsupported).
1154 */
tsc_page_update_unsafe(struct kvm_hv * hv)1155 static inline bool tsc_page_update_unsafe(struct kvm_hv *hv)
1156 {
1157 return (hv->hv_tsc_page_status != HV_TSC_PAGE_GUEST_CHANGED) &&
1158 hv->hv_tsc_emulation_control;
1159 }
1160
kvm_hv_setup_tsc_page(struct kvm * kvm,struct pvclock_vcpu_time_info * hv_clock)1161 void kvm_hv_setup_tsc_page(struct kvm *kvm,
1162 struct pvclock_vcpu_time_info *hv_clock)
1163 {
1164 struct kvm_hv *hv = to_kvm_hv(kvm);
1165 u32 tsc_seq;
1166 u64 gfn;
1167
1168 BUILD_BUG_ON(sizeof(tsc_seq) != sizeof(hv->tsc_ref.tsc_sequence));
1169 BUILD_BUG_ON(offsetof(struct ms_hyperv_tsc_page, tsc_sequence) != 0);
1170
1171 guard(mutex)(&hv->hv_lock);
1172
1173 if (hv->hv_tsc_page_status == HV_TSC_PAGE_BROKEN ||
1174 hv->hv_tsc_page_status == HV_TSC_PAGE_SET ||
1175 hv->hv_tsc_page_status == HV_TSC_PAGE_UNSET)
1176 return;
1177
1178 if (!(hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE))
1179 return;
1180
1181 gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT;
1182 /*
1183 * Because the TSC parameters only vary when there is a
1184 * change in the master clock, do not bother with caching.
1185 */
1186 if (unlikely(kvm_read_guest(kvm, gfn_to_gpa(gfn),
1187 &tsc_seq, sizeof(tsc_seq))))
1188 goto out_err;
1189
1190 if (tsc_seq && tsc_page_update_unsafe(hv)) {
1191 if (kvm_read_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
1192 goto out_err;
1193
1194 hv->hv_tsc_page_status = HV_TSC_PAGE_SET;
1195 return;
1196 }
1197
1198 /*
1199 * While we're computing and writing the parameters, force the
1200 * guest to use the time reference count MSR.
1201 */
1202 hv->tsc_ref.tsc_sequence = 0;
1203 if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
1204 &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
1205 goto out_err;
1206
1207 if (!compute_tsc_page_parameters(hv_clock, &hv->tsc_ref))
1208 goto out_err;
1209
1210 /* Ensure sequence is zero before writing the rest of the struct. */
1211 smp_wmb();
1212 if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref)))
1213 goto out_err;
1214
1215 /*
1216 * Now switch to the TSC page mechanism by writing the sequence.
1217 */
1218 tsc_seq++;
1219 if (tsc_seq == 0xFFFFFFFF || tsc_seq == 0)
1220 tsc_seq = 1;
1221
1222 /* Write the struct entirely before the non-zero sequence. */
1223 smp_wmb();
1224
1225 hv->tsc_ref.tsc_sequence = tsc_seq;
1226 if (kvm_write_guest(kvm, gfn_to_gpa(gfn),
1227 &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence)))
1228 goto out_err;
1229
1230 hv->hv_tsc_page_status = HV_TSC_PAGE_SET;
1231 return;
1232
1233 out_err:
1234 hv->hv_tsc_page_status = HV_TSC_PAGE_BROKEN;
1235 }
1236
kvm_hv_request_tsc_page_update(struct kvm * kvm)1237 void kvm_hv_request_tsc_page_update(struct kvm *kvm)
1238 {
1239 struct kvm_hv *hv = to_kvm_hv(kvm);
1240
1241 mutex_lock(&hv->hv_lock);
1242
1243 if (hv->hv_tsc_page_status == HV_TSC_PAGE_SET &&
1244 !tsc_page_update_unsafe(hv))
1245 hv->hv_tsc_page_status = HV_TSC_PAGE_HOST_CHANGED;
1246
1247 mutex_unlock(&hv->hv_lock);
1248 }
1249
hv_check_msr_access(struct kvm_vcpu_hv * hv_vcpu,u32 msr)1250 static bool hv_check_msr_access(struct kvm_vcpu_hv *hv_vcpu, u32 msr)
1251 {
1252 if (!hv_vcpu->enforce_cpuid)
1253 return true;
1254
1255 switch (msr) {
1256 case HV_X64_MSR_GUEST_OS_ID:
1257 case HV_X64_MSR_HYPERCALL:
1258 return hv_vcpu->cpuid_cache.features_eax &
1259 HV_MSR_HYPERCALL_AVAILABLE;
1260 case HV_X64_MSR_VP_RUNTIME:
1261 return hv_vcpu->cpuid_cache.features_eax &
1262 HV_MSR_VP_RUNTIME_AVAILABLE;
1263 case HV_X64_MSR_TIME_REF_COUNT:
1264 return hv_vcpu->cpuid_cache.features_eax &
1265 HV_MSR_TIME_REF_COUNT_AVAILABLE;
1266 case HV_X64_MSR_VP_INDEX:
1267 return hv_vcpu->cpuid_cache.features_eax &
1268 HV_MSR_VP_INDEX_AVAILABLE;
1269 case HV_X64_MSR_RESET:
1270 return hv_vcpu->cpuid_cache.features_eax &
1271 HV_MSR_RESET_AVAILABLE;
1272 case HV_X64_MSR_REFERENCE_TSC:
1273 return hv_vcpu->cpuid_cache.features_eax &
1274 HV_MSR_REFERENCE_TSC_AVAILABLE;
1275 case HV_X64_MSR_SCONTROL:
1276 case HV_X64_MSR_SVERSION:
1277 case HV_X64_MSR_SIEFP:
1278 case HV_X64_MSR_SIMP:
1279 case HV_X64_MSR_EOM:
1280 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
1281 return hv_vcpu->cpuid_cache.features_eax &
1282 HV_MSR_SYNIC_AVAILABLE;
1283 case HV_X64_MSR_STIMER0_CONFIG:
1284 case HV_X64_MSR_STIMER1_CONFIG:
1285 case HV_X64_MSR_STIMER2_CONFIG:
1286 case HV_X64_MSR_STIMER3_CONFIG:
1287 case HV_X64_MSR_STIMER0_COUNT:
1288 case HV_X64_MSR_STIMER1_COUNT:
1289 case HV_X64_MSR_STIMER2_COUNT:
1290 case HV_X64_MSR_STIMER3_COUNT:
1291 return hv_vcpu->cpuid_cache.features_eax &
1292 HV_MSR_SYNTIMER_AVAILABLE;
1293 case HV_X64_MSR_EOI:
1294 case HV_X64_MSR_ICR:
1295 case HV_X64_MSR_TPR:
1296 case HV_X64_MSR_VP_ASSIST_PAGE:
1297 return hv_vcpu->cpuid_cache.features_eax &
1298 HV_MSR_APIC_ACCESS_AVAILABLE;
1299 case HV_X64_MSR_TSC_FREQUENCY:
1300 case HV_X64_MSR_APIC_FREQUENCY:
1301 return hv_vcpu->cpuid_cache.features_eax &
1302 HV_ACCESS_FREQUENCY_MSRS;
1303 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1304 case HV_X64_MSR_TSC_EMULATION_CONTROL:
1305 case HV_X64_MSR_TSC_EMULATION_STATUS:
1306 return hv_vcpu->cpuid_cache.features_eax &
1307 HV_ACCESS_REENLIGHTENMENT;
1308 case HV_X64_MSR_TSC_INVARIANT_CONTROL:
1309 return hv_vcpu->cpuid_cache.features_eax &
1310 HV_ACCESS_TSC_INVARIANT;
1311 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1312 case HV_X64_MSR_CRASH_CTL:
1313 return hv_vcpu->cpuid_cache.features_edx &
1314 HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE;
1315 case HV_X64_MSR_SYNDBG_OPTIONS:
1316 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
1317 return hv_vcpu->cpuid_cache.features_edx &
1318 HV_FEATURE_DEBUG_MSRS_AVAILABLE;
1319 default:
1320 break;
1321 }
1322
1323 return false;
1324 }
1325
1326 #define KVM_HV_WIN2016_GUEST_ID 0x1040a00003839
1327 #define KVM_HV_WIN2016_GUEST_ID_MASK (~GENMASK_ULL(23, 16)) /* mask out the service version */
1328
1329 /*
1330 * Hyper-V enabled Windows Server 2016 SMP VMs fail to boot in !XSAVES && XSAVEC
1331 * configuration.
1332 * Such configuration can result from, for example, AMD Erratum 1386 workaround.
1333 *
1334 * Print a notice so users aren't left wondering what's suddenly gone wrong.
1335 */
__kvm_hv_xsaves_xsavec_maybe_warn(struct kvm_vcpu * vcpu)1336 static void __kvm_hv_xsaves_xsavec_maybe_warn(struct kvm_vcpu *vcpu)
1337 {
1338 struct kvm *kvm = vcpu->kvm;
1339 struct kvm_hv *hv = to_kvm_hv(kvm);
1340
1341 /* Check again under the hv_lock. */
1342 if (hv->xsaves_xsavec_checked)
1343 return;
1344
1345 if ((hv->hv_guest_os_id & KVM_HV_WIN2016_GUEST_ID_MASK) !=
1346 KVM_HV_WIN2016_GUEST_ID)
1347 return;
1348
1349 hv->xsaves_xsavec_checked = true;
1350
1351 /* UP configurations aren't affected */
1352 if (atomic_read(&kvm->online_vcpus) < 2)
1353 return;
1354
1355 if (guest_cpuid_has(vcpu, X86_FEATURE_XSAVES) ||
1356 !guest_cpu_cap_has(vcpu, X86_FEATURE_XSAVEC))
1357 return;
1358
1359 pr_notice_ratelimited("Booting SMP Windows KVM VM with !XSAVES && XSAVEC. "
1360 "If it fails to boot try disabling XSAVEC in the VM config.\n");
1361 }
1362
kvm_hv_xsaves_xsavec_maybe_warn(struct kvm_vcpu * vcpu)1363 void kvm_hv_xsaves_xsavec_maybe_warn(struct kvm_vcpu *vcpu)
1364 {
1365 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
1366
1367 if (!vcpu->arch.hyperv_enabled ||
1368 hv->xsaves_xsavec_checked)
1369 return;
1370
1371 mutex_lock(&hv->hv_lock);
1372 __kvm_hv_xsaves_xsavec_maybe_warn(vcpu);
1373 mutex_unlock(&hv->hv_lock);
1374 }
1375
kvm_hv_set_msr_pw(struct kvm_vcpu * vcpu,u32 msr,u64 data,bool host)1376 static int kvm_hv_set_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 data,
1377 bool host)
1378 {
1379 struct kvm *kvm = vcpu->kvm;
1380 struct kvm_hv *hv = to_kvm_hv(kvm);
1381
1382 if (unlikely(!host && !hv_check_msr_access(to_hv_vcpu(vcpu), msr)))
1383 return 1;
1384
1385 switch (msr) {
1386 case HV_X64_MSR_GUEST_OS_ID:
1387 hv->hv_guest_os_id = data;
1388 /* setting guest os id to zero disables hypercall page */
1389 if (!hv->hv_guest_os_id)
1390 hv->hv_hypercall &= ~HV_X64_MSR_HYPERCALL_ENABLE;
1391 break;
1392 case HV_X64_MSR_HYPERCALL: {
1393 u8 instructions[9];
1394 int i = 0;
1395 u64 addr;
1396
1397 /* if guest os id is not set hypercall should remain disabled */
1398 if (!hv->hv_guest_os_id)
1399 break;
1400 if (!(data & HV_X64_MSR_HYPERCALL_ENABLE)) {
1401 hv->hv_hypercall = data;
1402 break;
1403 }
1404
1405 /*
1406 * If Xen and Hyper-V hypercalls are both enabled, disambiguate
1407 * the same way Xen itself does, by setting the bit 31 of EAX
1408 * which is RsvdZ in the 32-bit Hyper-V hypercall ABI and just
1409 * going to be clobbered on 64-bit.
1410 */
1411 if (kvm_xen_hypercall_enabled(kvm)) {
1412 /* orl $0x80000000, %eax */
1413 instructions[i++] = 0x0d;
1414 instructions[i++] = 0x00;
1415 instructions[i++] = 0x00;
1416 instructions[i++] = 0x00;
1417 instructions[i++] = 0x80;
1418 }
1419
1420 /* vmcall/vmmcall */
1421 kvm_x86_call(patch_hypercall)(vcpu, instructions + i);
1422 i += 3;
1423
1424 /* ret */
1425 ((unsigned char *)instructions)[i++] = 0xc3;
1426
1427 addr = data & HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_MASK;
1428 if (kvm_vcpu_write_guest(vcpu, addr, instructions, i))
1429 return 1;
1430 hv->hv_hypercall = data;
1431 break;
1432 }
1433 case HV_X64_MSR_REFERENCE_TSC:
1434 hv->hv_tsc_page = data;
1435 if (hv->hv_tsc_page & HV_X64_MSR_TSC_REFERENCE_ENABLE) {
1436 if (!host)
1437 hv->hv_tsc_page_status = HV_TSC_PAGE_GUEST_CHANGED;
1438 else
1439 hv->hv_tsc_page_status = HV_TSC_PAGE_HOST_CHANGED;
1440 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1441 } else {
1442 hv->hv_tsc_page_status = HV_TSC_PAGE_UNSET;
1443 }
1444 break;
1445 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1446 return kvm_hv_msr_set_crash_data(kvm,
1447 msr - HV_X64_MSR_CRASH_P0,
1448 data);
1449 case HV_X64_MSR_CRASH_CTL:
1450 if (host)
1451 return kvm_hv_msr_set_crash_ctl(kvm, data);
1452
1453 if (data & HV_CRASH_CTL_CRASH_NOTIFY) {
1454 vcpu_debug(vcpu, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
1455 hv->hv_crash_param[0],
1456 hv->hv_crash_param[1],
1457 hv->hv_crash_param[2],
1458 hv->hv_crash_param[3],
1459 hv->hv_crash_param[4]);
1460
1461 /* Send notification about crash to user space */
1462 kvm_make_request(KVM_REQ_HV_CRASH, vcpu);
1463 }
1464 break;
1465 case HV_X64_MSR_RESET:
1466 if (data == 1) {
1467 vcpu_debug(vcpu, "hyper-v reset requested\n");
1468 kvm_make_request(KVM_REQ_HV_RESET, vcpu);
1469 }
1470 break;
1471 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1472 hv->hv_reenlightenment_control = data;
1473 break;
1474 case HV_X64_MSR_TSC_EMULATION_CONTROL:
1475 hv->hv_tsc_emulation_control = data;
1476 break;
1477 case HV_X64_MSR_TSC_EMULATION_STATUS:
1478 if (data && !host)
1479 return 1;
1480
1481 hv->hv_tsc_emulation_status = data;
1482 break;
1483 case HV_X64_MSR_TIME_REF_COUNT:
1484 /* read-only, but still ignore it if host-initiated */
1485 if (!host)
1486 return 1;
1487 break;
1488 case HV_X64_MSR_TSC_INVARIANT_CONTROL:
1489 /* Only bit 0 is supported */
1490 if (data & ~HV_EXPOSE_INVARIANT_TSC)
1491 return 1;
1492
1493 /* The feature can't be disabled from the guest */
1494 if (!host && hv->hv_invtsc_control && !data)
1495 return 1;
1496
1497 hv->hv_invtsc_control = data;
1498 break;
1499 case HV_X64_MSR_SYNDBG_OPTIONS:
1500 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
1501 return syndbg_set_msr(vcpu, msr, data, host);
1502 default:
1503 kvm_pr_unimpl_wrmsr(vcpu, msr, data);
1504 return 1;
1505 }
1506 return 0;
1507 }
1508
1509 /* Calculate cpu time spent by current task in 100ns units */
current_task_runtime_100ns(void)1510 static u64 current_task_runtime_100ns(void)
1511 {
1512 u64 utime, stime;
1513
1514 task_cputime_adjusted(current, &utime, &stime);
1515
1516 return div_u64(utime + stime, 100);
1517 }
1518
kvm_hv_set_msr(struct kvm_vcpu * vcpu,u32 msr,u64 data,bool host)1519 static int kvm_hv_set_msr(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
1520 {
1521 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
1522
1523 if (unlikely(!host && !hv_check_msr_access(hv_vcpu, msr)))
1524 return 1;
1525
1526 switch (msr) {
1527 case HV_X64_MSR_VP_INDEX: {
1528 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
1529 u32 new_vp_index = (u32)data;
1530
1531 if (!host || new_vp_index >= KVM_MAX_VCPUS)
1532 return 1;
1533
1534 if (new_vp_index == hv_vcpu->vp_index)
1535 return 0;
1536
1537 /*
1538 * The VP index is initialized to vcpu_index by
1539 * kvm_hv_vcpu_postcreate so they initially match. Now the
1540 * VP index is changing, adjust num_mismatched_vp_indexes if
1541 * it now matches or no longer matches vcpu_idx.
1542 */
1543 if (hv_vcpu->vp_index == vcpu->vcpu_idx)
1544 atomic_inc(&hv->num_mismatched_vp_indexes);
1545 else if (new_vp_index == vcpu->vcpu_idx)
1546 atomic_dec(&hv->num_mismatched_vp_indexes);
1547
1548 hv_vcpu->vp_index = new_vp_index;
1549 break;
1550 }
1551 case HV_X64_MSR_VP_ASSIST_PAGE: {
1552 u64 gfn;
1553 unsigned long addr;
1554
1555 if (!(data & HV_X64_MSR_VP_ASSIST_PAGE_ENABLE)) {
1556 hv_vcpu->hv_vapic = data;
1557 if (kvm_lapic_set_pv_eoi(vcpu, 0, 0))
1558 return 1;
1559 break;
1560 }
1561 gfn = data >> HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT;
1562 addr = kvm_vcpu_gfn_to_hva(vcpu, gfn);
1563 if (kvm_is_error_hva(addr))
1564 return 1;
1565
1566 /*
1567 * Clear apic_assist portion of struct hv_vp_assist_page
1568 * only, there can be valuable data in the rest which needs
1569 * to be preserved e.g. on migration.
1570 */
1571 if (put_user(0, (u32 __user *)addr))
1572 return 1;
1573 hv_vcpu->hv_vapic = data;
1574 kvm_vcpu_mark_page_dirty(vcpu, gfn);
1575 if (kvm_lapic_set_pv_eoi(vcpu,
1576 gfn_to_gpa(gfn) | KVM_MSR_ENABLED,
1577 sizeof(struct hv_vp_assist_page)))
1578 return 1;
1579 break;
1580 }
1581 case HV_X64_MSR_EOI:
1582 return kvm_hv_vapic_msr_write(vcpu, APIC_EOI, data);
1583 case HV_X64_MSR_ICR:
1584 return kvm_hv_vapic_msr_write(vcpu, APIC_ICR, data);
1585 case HV_X64_MSR_TPR:
1586 return kvm_hv_vapic_msr_write(vcpu, APIC_TASKPRI, data);
1587 case HV_X64_MSR_VP_RUNTIME:
1588 if (!host)
1589 return 1;
1590 hv_vcpu->runtime_offset = data - current_task_runtime_100ns();
1591 break;
1592 case HV_X64_MSR_SCONTROL:
1593 case HV_X64_MSR_SVERSION:
1594 case HV_X64_MSR_SIEFP:
1595 case HV_X64_MSR_SIMP:
1596 case HV_X64_MSR_EOM:
1597 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
1598 return synic_set_msr(to_hv_synic(vcpu), msr, data, host);
1599 case HV_X64_MSR_STIMER0_CONFIG:
1600 case HV_X64_MSR_STIMER1_CONFIG:
1601 case HV_X64_MSR_STIMER2_CONFIG:
1602 case HV_X64_MSR_STIMER3_CONFIG: {
1603 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
1604
1605 return stimer_set_config(to_hv_stimer(vcpu, timer_index),
1606 data, host);
1607 }
1608 case HV_X64_MSR_STIMER0_COUNT:
1609 case HV_X64_MSR_STIMER1_COUNT:
1610 case HV_X64_MSR_STIMER2_COUNT:
1611 case HV_X64_MSR_STIMER3_COUNT: {
1612 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
1613
1614 return stimer_set_count(to_hv_stimer(vcpu, timer_index),
1615 data, host);
1616 }
1617 case HV_X64_MSR_TSC_FREQUENCY:
1618 case HV_X64_MSR_APIC_FREQUENCY:
1619 /* read-only, but still ignore it if host-initiated */
1620 if (!host)
1621 return 1;
1622 break;
1623 default:
1624 kvm_pr_unimpl_wrmsr(vcpu, msr, data);
1625 return 1;
1626 }
1627
1628 return 0;
1629 }
1630
kvm_hv_get_msr_pw(struct kvm_vcpu * vcpu,u32 msr,u64 * pdata,bool host)1631 static int kvm_hv_get_msr_pw(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,
1632 bool host)
1633 {
1634 u64 data = 0;
1635 struct kvm *kvm = vcpu->kvm;
1636 struct kvm_hv *hv = to_kvm_hv(kvm);
1637
1638 if (unlikely(!host && !hv_check_msr_access(to_hv_vcpu(vcpu), msr)))
1639 return 1;
1640
1641 switch (msr) {
1642 case HV_X64_MSR_GUEST_OS_ID:
1643 data = hv->hv_guest_os_id;
1644 break;
1645 case HV_X64_MSR_HYPERCALL:
1646 data = hv->hv_hypercall;
1647 break;
1648 case HV_X64_MSR_TIME_REF_COUNT:
1649 data = get_time_ref_counter(kvm);
1650 break;
1651 case HV_X64_MSR_REFERENCE_TSC:
1652 data = hv->hv_tsc_page;
1653 break;
1654 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
1655 return kvm_hv_msr_get_crash_data(kvm,
1656 msr - HV_X64_MSR_CRASH_P0,
1657 pdata);
1658 case HV_X64_MSR_CRASH_CTL:
1659 return kvm_hv_msr_get_crash_ctl(kvm, pdata);
1660 case HV_X64_MSR_RESET:
1661 data = 0;
1662 break;
1663 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
1664 data = hv->hv_reenlightenment_control;
1665 break;
1666 case HV_X64_MSR_TSC_EMULATION_CONTROL:
1667 data = hv->hv_tsc_emulation_control;
1668 break;
1669 case HV_X64_MSR_TSC_EMULATION_STATUS:
1670 data = hv->hv_tsc_emulation_status;
1671 break;
1672 case HV_X64_MSR_TSC_INVARIANT_CONTROL:
1673 data = hv->hv_invtsc_control;
1674 break;
1675 case HV_X64_MSR_SYNDBG_OPTIONS:
1676 case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER:
1677 return syndbg_get_msr(vcpu, msr, pdata, host);
1678 default:
1679 kvm_pr_unimpl_rdmsr(vcpu, msr);
1680 return 1;
1681 }
1682
1683 *pdata = data;
1684 return 0;
1685 }
1686
kvm_hv_get_msr(struct kvm_vcpu * vcpu,u32 msr,u64 * pdata,bool host)1687 static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,
1688 bool host)
1689 {
1690 u64 data = 0;
1691 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
1692
1693 if (unlikely(!host && !hv_check_msr_access(hv_vcpu, msr)))
1694 return 1;
1695
1696 switch (msr) {
1697 case HV_X64_MSR_VP_INDEX:
1698 data = hv_vcpu->vp_index;
1699 break;
1700 case HV_X64_MSR_EOI:
1701 return kvm_hv_vapic_msr_read(vcpu, APIC_EOI, pdata);
1702 case HV_X64_MSR_ICR:
1703 return kvm_hv_vapic_msr_read(vcpu, APIC_ICR, pdata);
1704 case HV_X64_MSR_TPR:
1705 return kvm_hv_vapic_msr_read(vcpu, APIC_TASKPRI, pdata);
1706 case HV_X64_MSR_VP_ASSIST_PAGE:
1707 data = hv_vcpu->hv_vapic;
1708 break;
1709 case HV_X64_MSR_VP_RUNTIME:
1710 data = current_task_runtime_100ns() + hv_vcpu->runtime_offset;
1711 break;
1712 case HV_X64_MSR_SCONTROL:
1713 case HV_X64_MSR_SVERSION:
1714 case HV_X64_MSR_SIEFP:
1715 case HV_X64_MSR_SIMP:
1716 case HV_X64_MSR_EOM:
1717 case HV_X64_MSR_SINT0 ... HV_X64_MSR_SINT15:
1718 return synic_get_msr(to_hv_synic(vcpu), msr, pdata, host);
1719 case HV_X64_MSR_STIMER0_CONFIG:
1720 case HV_X64_MSR_STIMER1_CONFIG:
1721 case HV_X64_MSR_STIMER2_CONFIG:
1722 case HV_X64_MSR_STIMER3_CONFIG: {
1723 int timer_index = (msr - HV_X64_MSR_STIMER0_CONFIG)/2;
1724
1725 return stimer_get_config(to_hv_stimer(vcpu, timer_index),
1726 pdata);
1727 }
1728 case HV_X64_MSR_STIMER0_COUNT:
1729 case HV_X64_MSR_STIMER1_COUNT:
1730 case HV_X64_MSR_STIMER2_COUNT:
1731 case HV_X64_MSR_STIMER3_COUNT: {
1732 int timer_index = (msr - HV_X64_MSR_STIMER0_COUNT)/2;
1733
1734 return stimer_get_count(to_hv_stimer(vcpu, timer_index),
1735 pdata);
1736 }
1737 case HV_X64_MSR_TSC_FREQUENCY:
1738 data = (u64)vcpu->arch.virtual_tsc_khz * 1000;
1739 break;
1740 case HV_X64_MSR_APIC_FREQUENCY:
1741 data = div64_u64(1000000000ULL,
1742 vcpu->kvm->arch.apic_bus_cycle_ns);
1743 break;
1744 default:
1745 kvm_pr_unimpl_rdmsr(vcpu, msr);
1746 return 1;
1747 }
1748 *pdata = data;
1749 return 0;
1750 }
1751
kvm_hv_set_msr_common(struct kvm_vcpu * vcpu,u32 msr,u64 data,bool host)1752 int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)
1753 {
1754 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
1755
1756 if (!host && !vcpu->arch.hyperv_enabled)
1757 return 1;
1758
1759 if (kvm_hv_vcpu_init(vcpu))
1760 return 1;
1761
1762 if (kvm_hv_msr_partition_wide(msr)) {
1763 int r;
1764
1765 mutex_lock(&hv->hv_lock);
1766 r = kvm_hv_set_msr_pw(vcpu, msr, data, host);
1767 mutex_unlock(&hv->hv_lock);
1768 return r;
1769 } else
1770 return kvm_hv_set_msr(vcpu, msr, data, host);
1771 }
1772
kvm_hv_get_msr_common(struct kvm_vcpu * vcpu,u32 msr,u64 * pdata,bool host)1773 int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
1774 {
1775 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
1776
1777 if (!host && !vcpu->arch.hyperv_enabled)
1778 return 1;
1779
1780 if (kvm_hv_vcpu_init(vcpu))
1781 return 1;
1782
1783 if (kvm_hv_msr_partition_wide(msr)) {
1784 int r;
1785
1786 mutex_lock(&hv->hv_lock);
1787 r = kvm_hv_get_msr_pw(vcpu, msr, pdata, host);
1788 mutex_unlock(&hv->hv_lock);
1789 return r;
1790 } else
1791 return kvm_hv_get_msr(vcpu, msr, pdata, host);
1792 }
1793
sparse_set_to_vcpu_mask(struct kvm * kvm,u64 * sparse_banks,u64 valid_bank_mask,unsigned long * vcpu_mask)1794 static void sparse_set_to_vcpu_mask(struct kvm *kvm, u64 *sparse_banks,
1795 u64 valid_bank_mask, unsigned long *vcpu_mask)
1796 {
1797 struct kvm_hv *hv = to_kvm_hv(kvm);
1798 bool has_mismatch = atomic_read(&hv->num_mismatched_vp_indexes);
1799 u64 vp_bitmap[KVM_HV_MAX_SPARSE_VCPU_SET_BITS];
1800 struct kvm_vcpu *vcpu;
1801 int bank, sbank = 0;
1802 unsigned long i;
1803 u64 *bitmap;
1804
1805 BUILD_BUG_ON(sizeof(vp_bitmap) >
1806 sizeof(*vcpu_mask) * BITS_TO_LONGS(KVM_MAX_VCPUS));
1807
1808 /*
1809 * If vp_index == vcpu_idx for all vCPUs, fill vcpu_mask directly, else
1810 * fill a temporary buffer and manually test each vCPU's VP index.
1811 */
1812 if (likely(!has_mismatch))
1813 bitmap = (u64 *)vcpu_mask;
1814 else
1815 bitmap = vp_bitmap;
1816
1817 /*
1818 * Each set of 64 VPs is packed into sparse_banks, with valid_bank_mask
1819 * having a '1' for each bank that exists in sparse_banks. Sets must
1820 * be in ascending order, i.e. bank0..bankN.
1821 */
1822 memset(bitmap, 0, sizeof(vp_bitmap));
1823 for_each_set_bit(bank, (unsigned long *)&valid_bank_mask,
1824 KVM_HV_MAX_SPARSE_VCPU_SET_BITS)
1825 bitmap[bank] = sparse_banks[sbank++];
1826
1827 if (likely(!has_mismatch))
1828 return;
1829
1830 bitmap_zero(vcpu_mask, KVM_MAX_VCPUS);
1831 kvm_for_each_vcpu(i, vcpu, kvm) {
1832 if (test_bit(kvm_hv_get_vpindex(vcpu), (unsigned long *)vp_bitmap))
1833 __set_bit(i, vcpu_mask);
1834 }
1835 }
1836
hv_is_vp_in_sparse_set(u32 vp_id,u64 valid_bank_mask,u64 sparse_banks[])1837 static bool hv_is_vp_in_sparse_set(u32 vp_id, u64 valid_bank_mask, u64 sparse_banks[])
1838 {
1839 int valid_bit_nr = vp_id / HV_VCPUS_PER_SPARSE_BANK;
1840 unsigned long sbank;
1841
1842 if (!test_bit(valid_bit_nr, (unsigned long *)&valid_bank_mask))
1843 return false;
1844
1845 /*
1846 * The index into the sparse bank is the number of preceding bits in
1847 * the valid mask. Optimize for VMs with <64 vCPUs by skipping the
1848 * fancy math if there can't possibly be preceding bits.
1849 */
1850 if (valid_bit_nr)
1851 sbank = hweight64(valid_bank_mask & GENMASK_ULL(valid_bit_nr - 1, 0));
1852 else
1853 sbank = 0;
1854
1855 return test_bit(vp_id % HV_VCPUS_PER_SPARSE_BANK,
1856 (unsigned long *)&sparse_banks[sbank]);
1857 }
1858
1859 struct kvm_hv_hcall {
1860 /* Hypercall input data */
1861 u64 param;
1862 u64 ingpa;
1863 u64 outgpa;
1864 u16 code;
1865 u16 var_cnt;
1866 u16 rep_cnt;
1867 u16 rep_idx;
1868 bool fast;
1869 bool rep;
1870 sse128_t xmm[HV_HYPERCALL_MAX_XMM_REGISTERS];
1871
1872 /*
1873 * Current read offset when KVM reads hypercall input data gradually,
1874 * either offset in bytes from 'ingpa' for regular hypercalls or the
1875 * number of already consumed 'XMM halves' for 'fast' hypercalls.
1876 */
1877 union {
1878 gpa_t data_offset;
1879 int consumed_xmm_halves;
1880 };
1881 };
1882
1883
kvm_hv_get_hc_data(struct kvm * kvm,struct kvm_hv_hcall * hc,u16 orig_cnt,u16 cnt_cap,u64 * data)1884 static int kvm_hv_get_hc_data(struct kvm *kvm, struct kvm_hv_hcall *hc,
1885 u16 orig_cnt, u16 cnt_cap, u64 *data)
1886 {
1887 /*
1888 * Preserve the original count when ignoring entries via a "cap", KVM
1889 * still needs to validate the guest input (though the non-XMM path
1890 * punts on the checks).
1891 */
1892 u16 cnt = min(orig_cnt, cnt_cap);
1893 int i, j;
1894
1895 if (hc->fast) {
1896 /*
1897 * Each XMM holds two sparse banks, but do not count halves that
1898 * have already been consumed for hypercall parameters.
1899 */
1900 if (orig_cnt > 2 * HV_HYPERCALL_MAX_XMM_REGISTERS - hc->consumed_xmm_halves)
1901 return HV_STATUS_INVALID_HYPERCALL_INPUT;
1902
1903 for (i = 0; i < cnt; i++) {
1904 j = i + hc->consumed_xmm_halves;
1905 if (j % 2)
1906 data[i] = sse128_hi(hc->xmm[j / 2]);
1907 else
1908 data[i] = sse128_lo(hc->xmm[j / 2]);
1909 }
1910 return 0;
1911 }
1912
1913 return kvm_read_guest(kvm, hc->ingpa + hc->data_offset, data,
1914 cnt * sizeof(*data));
1915 }
1916
kvm_get_sparse_vp_set(struct kvm * kvm,struct kvm_hv_hcall * hc,u64 * sparse_banks)1917 static u64 kvm_get_sparse_vp_set(struct kvm *kvm, struct kvm_hv_hcall *hc,
1918 u64 *sparse_banks)
1919 {
1920 if (hc->var_cnt > HV_MAX_SPARSE_VCPU_BANKS)
1921 return -EINVAL;
1922
1923 /* Cap var_cnt to ignore banks that cannot contain a legal VP index. */
1924 return kvm_hv_get_hc_data(kvm, hc, hc->var_cnt, KVM_HV_MAX_SPARSE_VCPU_SET_BITS,
1925 sparse_banks);
1926 }
1927
kvm_hv_get_tlb_flush_entries(struct kvm * kvm,struct kvm_hv_hcall * hc,u64 entries[])1928 static int kvm_hv_get_tlb_flush_entries(struct kvm *kvm, struct kvm_hv_hcall *hc, u64 entries[])
1929 {
1930 return kvm_hv_get_hc_data(kvm, hc, hc->rep_cnt, hc->rep_cnt, entries);
1931 }
1932
hv_tlb_flush_enqueue(struct kvm_vcpu * vcpu,struct kvm_vcpu_hv_tlb_flush_fifo * tlb_flush_fifo,u64 * entries,int count)1933 static void hv_tlb_flush_enqueue(struct kvm_vcpu *vcpu,
1934 struct kvm_vcpu_hv_tlb_flush_fifo *tlb_flush_fifo,
1935 u64 *entries, int count)
1936 {
1937 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
1938 u64 flush_all_entry = KVM_HV_TLB_FLUSHALL_ENTRY;
1939
1940 if (!hv_vcpu)
1941 return;
1942
1943 spin_lock(&tlb_flush_fifo->write_lock);
1944
1945 /*
1946 * All entries should fit on the fifo leaving one free for 'flush all'
1947 * entry in case another request comes in. In case there's not enough
1948 * space, just put 'flush all' entry there.
1949 */
1950 if (count && entries && count < kfifo_avail(&tlb_flush_fifo->entries)) {
1951 WARN_ON(kfifo_in(&tlb_flush_fifo->entries, entries, count) != count);
1952 goto out_unlock;
1953 }
1954
1955 /*
1956 * Note: full fifo always contains 'flush all' entry, no need to check the
1957 * return value.
1958 */
1959 kfifo_in(&tlb_flush_fifo->entries, &flush_all_entry, 1);
1960
1961 out_unlock:
1962 spin_unlock(&tlb_flush_fifo->write_lock);
1963 }
1964
kvm_hv_vcpu_flush_tlb(struct kvm_vcpu * vcpu)1965 int kvm_hv_vcpu_flush_tlb(struct kvm_vcpu *vcpu)
1966 {
1967 struct kvm_vcpu_hv_tlb_flush_fifo *tlb_flush_fifo;
1968 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
1969 u64 entries[KVM_HV_TLB_FLUSH_FIFO_SIZE];
1970 int i, j, count;
1971 gva_t gva;
1972
1973 if (!tdp_enabled || !hv_vcpu)
1974 return -EINVAL;
1975
1976 tlb_flush_fifo = kvm_hv_get_tlb_flush_fifo(vcpu, is_guest_mode(vcpu));
1977
1978 count = kfifo_out(&tlb_flush_fifo->entries, entries, KVM_HV_TLB_FLUSH_FIFO_SIZE);
1979
1980 for (i = 0; i < count; i++) {
1981 if (entries[i] == KVM_HV_TLB_FLUSHALL_ENTRY)
1982 goto out_flush_all;
1983
1984 /*
1985 * Lower 12 bits of 'address' encode the number of additional
1986 * pages to flush.
1987 */
1988 gva = entries[i] & PAGE_MASK;
1989 for (j = 0; j < (entries[i] & ~PAGE_MASK) + 1; j++) {
1990 if (is_noncanonical_invlpg_address(gva + j * PAGE_SIZE, vcpu))
1991 continue;
1992
1993 kvm_x86_call(flush_tlb_gva)(vcpu, gva + j * PAGE_SIZE);
1994 }
1995
1996 ++vcpu->stat.tlb_flush;
1997 }
1998 return 0;
1999
2000 out_flush_all:
2001 kfifo_reset_out(&tlb_flush_fifo->entries);
2002
2003 /* Fall back to full flush. */
2004 return -ENOSPC;
2005 }
2006
kvm_hv_flush_tlb(struct kvm_vcpu * vcpu,struct kvm_hv_hcall * hc)2007 static u64 kvm_hv_flush_tlb(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
2008 {
2009 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
2010 unsigned long *vcpu_mask = hv_vcpu->vcpu_mask;
2011 u64 *sparse_banks = hv_vcpu->sparse_banks;
2012 struct kvm *kvm = vcpu->kvm;
2013 struct hv_tlb_flush_ex flush_ex;
2014 struct hv_tlb_flush flush;
2015 struct kvm_vcpu_hv_tlb_flush_fifo *tlb_flush_fifo;
2016 /*
2017 * Normally, there can be no more than 'KVM_HV_TLB_FLUSH_FIFO_SIZE'
2018 * entries on the TLB flush fifo. The last entry, however, needs to be
2019 * always left free for 'flush all' entry which gets placed when
2020 * there is not enough space to put all the requested entries.
2021 */
2022 u64 __tlb_flush_entries[KVM_HV_TLB_FLUSH_FIFO_SIZE - 1];
2023 u64 *tlb_flush_entries;
2024 u64 valid_bank_mask;
2025 struct kvm_vcpu *v;
2026 unsigned long i;
2027 bool all_cpus;
2028
2029 /*
2030 * The Hyper-V TLFS doesn't allow more than HV_MAX_SPARSE_VCPU_BANKS
2031 * sparse banks. Fail the build if KVM's max allowed number of
2032 * vCPUs (>4096) exceeds this limit.
2033 */
2034 BUILD_BUG_ON(KVM_HV_MAX_SPARSE_VCPU_SET_BITS > HV_MAX_SPARSE_VCPU_BANKS);
2035
2036 /*
2037 * 'Slow' hypercall's first parameter is the address in guest's memory
2038 * where hypercall parameters are placed. This is either a GPA or a
2039 * nested GPA when KVM is handling the call from L2 ('direct' TLB
2040 * flush). Translate the address here so the memory can be uniformly
2041 * read with kvm_read_guest().
2042 */
2043 if (!hc->fast && is_guest_mode(vcpu)) {
2044 hc->ingpa = translate_nested_gpa(vcpu, hc->ingpa, 0, NULL);
2045 if (unlikely(hc->ingpa == INVALID_GPA))
2046 return HV_STATUS_INVALID_HYPERCALL_INPUT;
2047 }
2048
2049 if (hc->code == HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST ||
2050 hc->code == HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE) {
2051 if (hc->fast) {
2052 flush.address_space = hc->ingpa;
2053 flush.flags = hc->outgpa;
2054 flush.processor_mask = sse128_lo(hc->xmm[0]);
2055 hc->consumed_xmm_halves = 1;
2056 } else {
2057 if (unlikely(kvm_read_guest(kvm, hc->ingpa,
2058 &flush, sizeof(flush))))
2059 return HV_STATUS_INVALID_HYPERCALL_INPUT;
2060 hc->data_offset = sizeof(flush);
2061 }
2062
2063 trace_kvm_hv_flush_tlb(flush.processor_mask,
2064 flush.address_space, flush.flags,
2065 is_guest_mode(vcpu));
2066
2067 valid_bank_mask = BIT_ULL(0);
2068 sparse_banks[0] = flush.processor_mask;
2069
2070 /*
2071 * Work around possible WS2012 bug: it sends hypercalls
2072 * with processor_mask = 0x0 and HV_FLUSH_ALL_PROCESSORS clear,
2073 * while also expecting us to flush something and crashing if
2074 * we don't. Let's treat processor_mask == 0 same as
2075 * HV_FLUSH_ALL_PROCESSORS.
2076 */
2077 all_cpus = (flush.flags & HV_FLUSH_ALL_PROCESSORS) ||
2078 flush.processor_mask == 0;
2079 } else {
2080 if (hc->fast) {
2081 flush_ex.address_space = hc->ingpa;
2082 flush_ex.flags = hc->outgpa;
2083 memcpy(&flush_ex.hv_vp_set,
2084 &hc->xmm[0], sizeof(hc->xmm[0]));
2085 hc->consumed_xmm_halves = 2;
2086 } else {
2087 if (unlikely(kvm_read_guest(kvm, hc->ingpa, &flush_ex,
2088 sizeof(flush_ex))))
2089 return HV_STATUS_INVALID_HYPERCALL_INPUT;
2090 hc->data_offset = sizeof(flush_ex);
2091 }
2092
2093 trace_kvm_hv_flush_tlb_ex(flush_ex.hv_vp_set.valid_bank_mask,
2094 flush_ex.hv_vp_set.format,
2095 flush_ex.address_space,
2096 flush_ex.flags, is_guest_mode(vcpu));
2097
2098 valid_bank_mask = flush_ex.hv_vp_set.valid_bank_mask;
2099 all_cpus = flush_ex.hv_vp_set.format !=
2100 HV_GENERIC_SET_SPARSE_4K;
2101
2102 if (hc->var_cnt != hweight64(valid_bank_mask))
2103 return HV_STATUS_INVALID_HYPERCALL_INPUT;
2104
2105 if (!all_cpus) {
2106 if (!hc->var_cnt)
2107 goto ret_success;
2108
2109 if (kvm_get_sparse_vp_set(kvm, hc, sparse_banks))
2110 return HV_STATUS_INVALID_HYPERCALL_INPUT;
2111 }
2112
2113 /*
2114 * Hyper-V TLFS doesn't explicitly forbid non-empty sparse vCPU
2115 * banks (and, thus, non-zero 'var_cnt') for the 'all vCPUs'
2116 * case (HV_GENERIC_SET_ALL). Always adjust data_offset and
2117 * consumed_xmm_halves to make sure TLB flush entries are read
2118 * from the correct offset.
2119 */
2120 if (hc->fast)
2121 hc->consumed_xmm_halves += hc->var_cnt;
2122 else
2123 hc->data_offset += hc->var_cnt * sizeof(sparse_banks[0]);
2124 }
2125
2126 if (hc->code == HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE ||
2127 hc->code == HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX ||
2128 hc->rep_cnt > ARRAY_SIZE(__tlb_flush_entries)) {
2129 tlb_flush_entries = NULL;
2130 } else {
2131 if (kvm_hv_get_tlb_flush_entries(kvm, hc, __tlb_flush_entries))
2132 return HV_STATUS_INVALID_HYPERCALL_INPUT;
2133 tlb_flush_entries = __tlb_flush_entries;
2134 }
2135
2136 /*
2137 * vcpu->arch.cr3 may not be up-to-date for running vCPUs so we can't
2138 * analyze it here, flush TLB regardless of the specified address space.
2139 */
2140 if (all_cpus && !is_guest_mode(vcpu)) {
2141 kvm_for_each_vcpu(i, v, kvm) {
2142 tlb_flush_fifo = kvm_hv_get_tlb_flush_fifo(v, false);
2143 hv_tlb_flush_enqueue(v, tlb_flush_fifo,
2144 tlb_flush_entries, hc->rep_cnt);
2145 }
2146
2147 kvm_make_all_cpus_request(kvm, KVM_REQ_HV_TLB_FLUSH);
2148 } else if (!is_guest_mode(vcpu)) {
2149 sparse_set_to_vcpu_mask(kvm, sparse_banks, valid_bank_mask, vcpu_mask);
2150
2151 for_each_set_bit(i, vcpu_mask, KVM_MAX_VCPUS) {
2152 v = kvm_get_vcpu(kvm, i);
2153 if (!v)
2154 continue;
2155 tlb_flush_fifo = kvm_hv_get_tlb_flush_fifo(v, false);
2156 hv_tlb_flush_enqueue(v, tlb_flush_fifo,
2157 tlb_flush_entries, hc->rep_cnt);
2158 }
2159
2160 kvm_make_vcpus_request_mask(kvm, KVM_REQ_HV_TLB_FLUSH, vcpu_mask);
2161 } else {
2162 struct kvm_vcpu_hv *hv_v;
2163
2164 bitmap_zero(vcpu_mask, KVM_MAX_VCPUS);
2165
2166 kvm_for_each_vcpu(i, v, kvm) {
2167 hv_v = to_hv_vcpu(v);
2168
2169 /*
2170 * The following check races with nested vCPUs entering/exiting
2171 * and/or migrating between L1's vCPUs, however the only case when
2172 * KVM *must* flush the TLB is when the target L2 vCPU keeps
2173 * running on the same L1 vCPU from the moment of the request until
2174 * kvm_hv_flush_tlb() returns. TLB is fully flushed in all other
2175 * cases, e.g. when the target L2 vCPU migrates to a different L1
2176 * vCPU or when the corresponding L1 vCPU temporary switches to a
2177 * different L2 vCPU while the request is being processed.
2178 */
2179 if (!hv_v || hv_v->nested.vm_id != hv_vcpu->nested.vm_id)
2180 continue;
2181
2182 if (!all_cpus &&
2183 !hv_is_vp_in_sparse_set(hv_v->nested.vp_id, valid_bank_mask,
2184 sparse_banks))
2185 continue;
2186
2187 __set_bit(i, vcpu_mask);
2188 tlb_flush_fifo = kvm_hv_get_tlb_flush_fifo(v, true);
2189 hv_tlb_flush_enqueue(v, tlb_flush_fifo,
2190 tlb_flush_entries, hc->rep_cnt);
2191 }
2192
2193 kvm_make_vcpus_request_mask(kvm, KVM_REQ_HV_TLB_FLUSH, vcpu_mask);
2194 }
2195
2196 ret_success:
2197 /* We always do full TLB flush, set 'Reps completed' = 'Rep Count' */
2198 return (u64)HV_STATUS_SUCCESS |
2199 ((u64)hc->rep_cnt << HV_HYPERCALL_REP_COMP_OFFSET);
2200 }
2201
kvm_hv_send_ipi_to_many(struct kvm * kvm,u32 vector,u64 * sparse_banks,u64 valid_bank_mask)2202 static void kvm_hv_send_ipi_to_many(struct kvm *kvm, u32 vector,
2203 u64 *sparse_banks, u64 valid_bank_mask)
2204 {
2205 struct kvm_lapic_irq irq = {
2206 .delivery_mode = APIC_DM_FIXED,
2207 .vector = vector
2208 };
2209 struct kvm_vcpu *vcpu;
2210 unsigned long i;
2211
2212 kvm_for_each_vcpu(i, vcpu, kvm) {
2213 if (sparse_banks &&
2214 !hv_is_vp_in_sparse_set(kvm_hv_get_vpindex(vcpu),
2215 valid_bank_mask, sparse_banks))
2216 continue;
2217
2218 /* We fail only when APIC is disabled */
2219 kvm_apic_set_irq(vcpu, &irq, NULL);
2220 }
2221 }
2222
kvm_hv_send_ipi(struct kvm_vcpu * vcpu,struct kvm_hv_hcall * hc)2223 static u64 kvm_hv_send_ipi(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
2224 {
2225 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
2226 u64 *sparse_banks = hv_vcpu->sparse_banks;
2227 struct kvm *kvm = vcpu->kvm;
2228 struct hv_send_ipi_ex send_ipi_ex;
2229 struct hv_send_ipi send_ipi;
2230 u64 valid_bank_mask;
2231 u32 vector;
2232 bool all_cpus;
2233
2234 if (!lapic_in_kernel(vcpu))
2235 return HV_STATUS_INVALID_HYPERCALL_INPUT;
2236
2237 if (hc->code == HVCALL_SEND_IPI) {
2238 if (!hc->fast) {
2239 if (unlikely(kvm_read_guest(kvm, hc->ingpa, &send_ipi,
2240 sizeof(send_ipi))))
2241 return HV_STATUS_INVALID_HYPERCALL_INPUT;
2242 sparse_banks[0] = send_ipi.cpu_mask;
2243 vector = send_ipi.vector;
2244 } else {
2245 /* 'reserved' part of hv_send_ipi should be 0 */
2246 if (unlikely(hc->ingpa >> 32 != 0))
2247 return HV_STATUS_INVALID_HYPERCALL_INPUT;
2248 sparse_banks[0] = hc->outgpa;
2249 vector = (u32)hc->ingpa;
2250 }
2251 all_cpus = false;
2252 valid_bank_mask = BIT_ULL(0);
2253
2254 trace_kvm_hv_send_ipi(vector, sparse_banks[0]);
2255 } else {
2256 if (!hc->fast) {
2257 if (unlikely(kvm_read_guest(kvm, hc->ingpa, &send_ipi_ex,
2258 sizeof(send_ipi_ex))))
2259 return HV_STATUS_INVALID_HYPERCALL_INPUT;
2260 } else {
2261 send_ipi_ex.vector = (u32)hc->ingpa;
2262 send_ipi_ex.vp_set.format = hc->outgpa;
2263 send_ipi_ex.vp_set.valid_bank_mask = sse128_lo(hc->xmm[0]);
2264 }
2265
2266 trace_kvm_hv_send_ipi_ex(send_ipi_ex.vector,
2267 send_ipi_ex.vp_set.format,
2268 send_ipi_ex.vp_set.valid_bank_mask);
2269
2270 vector = send_ipi_ex.vector;
2271 valid_bank_mask = send_ipi_ex.vp_set.valid_bank_mask;
2272 all_cpus = send_ipi_ex.vp_set.format == HV_GENERIC_SET_ALL;
2273
2274 if (hc->var_cnt != hweight64(valid_bank_mask))
2275 return HV_STATUS_INVALID_HYPERCALL_INPUT;
2276
2277 if (all_cpus)
2278 goto check_and_send_ipi;
2279
2280 if (!hc->var_cnt)
2281 goto ret_success;
2282
2283 if (!hc->fast)
2284 hc->data_offset = offsetof(struct hv_send_ipi_ex,
2285 vp_set.bank_contents);
2286 else
2287 hc->consumed_xmm_halves = 1;
2288
2289 if (kvm_get_sparse_vp_set(kvm, hc, sparse_banks))
2290 return HV_STATUS_INVALID_HYPERCALL_INPUT;
2291 }
2292
2293 check_and_send_ipi:
2294 if ((vector < HV_IPI_LOW_VECTOR) || (vector > HV_IPI_HIGH_VECTOR))
2295 return HV_STATUS_INVALID_HYPERCALL_INPUT;
2296
2297 if (all_cpus)
2298 kvm_hv_send_ipi_to_many(kvm, vector, NULL, 0);
2299 else
2300 kvm_hv_send_ipi_to_many(kvm, vector, sparse_banks, valid_bank_mask);
2301
2302 ret_success:
2303 return HV_STATUS_SUCCESS;
2304 }
2305
kvm_hv_set_cpuid(struct kvm_vcpu * vcpu,bool hyperv_enabled)2306 void kvm_hv_set_cpuid(struct kvm_vcpu *vcpu, bool hyperv_enabled)
2307 {
2308 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
2309 struct kvm_cpuid_entry2 *entry;
2310
2311 vcpu->arch.hyperv_enabled = hyperv_enabled;
2312
2313 if (!hv_vcpu) {
2314 /*
2315 * KVM should have already allocated kvm_vcpu_hv if Hyper-V is
2316 * enabled in CPUID.
2317 */
2318 WARN_ON_ONCE(vcpu->arch.hyperv_enabled);
2319 return;
2320 }
2321
2322 memset(&hv_vcpu->cpuid_cache, 0, sizeof(hv_vcpu->cpuid_cache));
2323
2324 if (!vcpu->arch.hyperv_enabled)
2325 return;
2326
2327 entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_FEATURES);
2328 if (entry) {
2329 hv_vcpu->cpuid_cache.features_eax = entry->eax;
2330 hv_vcpu->cpuid_cache.features_ebx = entry->ebx;
2331 hv_vcpu->cpuid_cache.features_edx = entry->edx;
2332 }
2333
2334 entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_ENLIGHTMENT_INFO);
2335 if (entry) {
2336 hv_vcpu->cpuid_cache.enlightenments_eax = entry->eax;
2337 hv_vcpu->cpuid_cache.enlightenments_ebx = entry->ebx;
2338 }
2339
2340 entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES);
2341 if (entry)
2342 hv_vcpu->cpuid_cache.syndbg_cap_eax = entry->eax;
2343
2344 entry = kvm_find_cpuid_entry(vcpu, HYPERV_CPUID_NESTED_FEATURES);
2345 if (entry) {
2346 hv_vcpu->cpuid_cache.nested_eax = entry->eax;
2347 hv_vcpu->cpuid_cache.nested_ebx = entry->ebx;
2348 }
2349 }
2350
kvm_hv_set_enforce_cpuid(struct kvm_vcpu * vcpu,bool enforce)2351 int kvm_hv_set_enforce_cpuid(struct kvm_vcpu *vcpu, bool enforce)
2352 {
2353 struct kvm_vcpu_hv *hv_vcpu;
2354 int ret = 0;
2355
2356 if (!to_hv_vcpu(vcpu)) {
2357 if (enforce) {
2358 ret = kvm_hv_vcpu_init(vcpu);
2359 if (ret)
2360 return ret;
2361 } else {
2362 return 0;
2363 }
2364 }
2365
2366 hv_vcpu = to_hv_vcpu(vcpu);
2367 hv_vcpu->enforce_cpuid = enforce;
2368
2369 return ret;
2370 }
2371
kvm_hv_hypercall_set_result(struct kvm_vcpu * vcpu,u64 result)2372 static void kvm_hv_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
2373 {
2374 bool longmode;
2375
2376 longmode = is_64_bit_hypercall(vcpu);
2377 if (longmode)
2378 kvm_rax_write(vcpu, result);
2379 else {
2380 kvm_rdx_write(vcpu, result >> 32);
2381 kvm_rax_write(vcpu, result & 0xffffffff);
2382 }
2383 }
2384
kvm_hv_hypercall_complete(struct kvm_vcpu * vcpu,u64 result)2385 static int kvm_hv_hypercall_complete(struct kvm_vcpu *vcpu, u64 result)
2386 {
2387 u32 tlb_lock_count = 0;
2388 int ret;
2389
2390 if (hv_result_success(result) && is_guest_mode(vcpu) &&
2391 kvm_hv_is_tlb_flush_hcall(vcpu) &&
2392 kvm_read_guest(vcpu->kvm, to_hv_vcpu(vcpu)->nested.pa_page_gpa,
2393 &tlb_lock_count, sizeof(tlb_lock_count)))
2394 result = HV_STATUS_INVALID_HYPERCALL_INPUT;
2395
2396 trace_kvm_hv_hypercall_done(result);
2397 kvm_hv_hypercall_set_result(vcpu, result);
2398 ++vcpu->stat.hypercalls;
2399
2400 ret = kvm_skip_emulated_instruction(vcpu);
2401
2402 if (tlb_lock_count)
2403 kvm_x86_ops.nested_ops->hv_inject_synthetic_vmexit_post_tlb_flush(vcpu);
2404
2405 return ret;
2406 }
2407
kvm_hv_hypercall_complete_userspace(struct kvm_vcpu * vcpu)2408 static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
2409 {
2410 return kvm_hv_hypercall_complete(vcpu, vcpu->run->hyperv.u.hcall.result);
2411 }
2412
kvm_hvcall_signal_event(struct kvm_vcpu * vcpu,struct kvm_hv_hcall * hc)2413 static u16 kvm_hvcall_signal_event(struct kvm_vcpu *vcpu, struct kvm_hv_hcall *hc)
2414 {
2415 struct kvm_hv *hv = to_kvm_hv(vcpu->kvm);
2416 struct eventfd_ctx *eventfd;
2417
2418 if (unlikely(!hc->fast)) {
2419 int ret;
2420 gpa_t gpa = hc->ingpa;
2421
2422 if ((gpa & (__alignof__(hc->ingpa) - 1)) ||
2423 offset_in_page(gpa) + sizeof(hc->ingpa) > PAGE_SIZE)
2424 return HV_STATUS_INVALID_ALIGNMENT;
2425
2426 ret = kvm_vcpu_read_guest(vcpu, gpa,
2427 &hc->ingpa, sizeof(hc->ingpa));
2428 if (ret < 0)
2429 return HV_STATUS_INVALID_ALIGNMENT;
2430 }
2431
2432 /*
2433 * Per spec, bits 32-47 contain the extra "flag number". However, we
2434 * have no use for it, and in all known usecases it is zero, so just
2435 * report lookup failure if it isn't.
2436 */
2437 if (hc->ingpa & 0xffff00000000ULL)
2438 return HV_STATUS_INVALID_PORT_ID;
2439 /* remaining bits are reserved-zero */
2440 if (hc->ingpa & ~KVM_HYPERV_CONN_ID_MASK)
2441 return HV_STATUS_INVALID_HYPERCALL_INPUT;
2442
2443 /* the eventfd is protected by vcpu->kvm->srcu, but conn_to_evt isn't */
2444 rcu_read_lock();
2445 eventfd = idr_find(&hv->conn_to_evt, hc->ingpa);
2446 rcu_read_unlock();
2447 if (!eventfd)
2448 return HV_STATUS_INVALID_PORT_ID;
2449
2450 eventfd_signal(eventfd);
2451 return HV_STATUS_SUCCESS;
2452 }
2453
is_xmm_fast_hypercall(struct kvm_hv_hcall * hc)2454 static bool is_xmm_fast_hypercall(struct kvm_hv_hcall *hc)
2455 {
2456 switch (hc->code) {
2457 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST:
2458 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE:
2459 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX:
2460 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX:
2461 case HVCALL_SEND_IPI_EX:
2462 return true;
2463 }
2464
2465 return false;
2466 }
2467
kvm_hv_hypercall_read_xmm(struct kvm_hv_hcall * hc)2468 static void kvm_hv_hypercall_read_xmm(struct kvm_hv_hcall *hc)
2469 {
2470 int reg;
2471
2472 kvm_fpu_get();
2473 for (reg = 0; reg < HV_HYPERCALL_MAX_XMM_REGISTERS; reg++)
2474 _kvm_read_sse_reg(reg, &hc->xmm[reg]);
2475 kvm_fpu_put();
2476 }
2477
hv_check_hypercall_access(struct kvm_vcpu_hv * hv_vcpu,u16 code)2478 static bool hv_check_hypercall_access(struct kvm_vcpu_hv *hv_vcpu, u16 code)
2479 {
2480 if (!hv_vcpu->enforce_cpuid)
2481 return true;
2482
2483 switch (code) {
2484 case HVCALL_NOTIFY_LONG_SPIN_WAIT:
2485 return hv_vcpu->cpuid_cache.enlightenments_ebx &&
2486 hv_vcpu->cpuid_cache.enlightenments_ebx != U32_MAX;
2487 case HVCALL_POST_MESSAGE:
2488 return hv_vcpu->cpuid_cache.features_ebx & HV_POST_MESSAGES;
2489 case HVCALL_SIGNAL_EVENT:
2490 return hv_vcpu->cpuid_cache.features_ebx & HV_SIGNAL_EVENTS;
2491 case HVCALL_POST_DEBUG_DATA:
2492 case HVCALL_RETRIEVE_DEBUG_DATA:
2493 case HVCALL_RESET_DEBUG_SESSION:
2494 /*
2495 * Return 'true' when SynDBG is disabled so the resulting code
2496 * will be HV_STATUS_INVALID_HYPERCALL_CODE.
2497 */
2498 return !kvm_hv_is_syndbg_enabled(hv_vcpu->vcpu) ||
2499 hv_vcpu->cpuid_cache.features_ebx & HV_DEBUGGING;
2500 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX:
2501 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX:
2502 if (!(hv_vcpu->cpuid_cache.enlightenments_eax &
2503 HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED))
2504 return false;
2505 fallthrough;
2506 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST:
2507 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE:
2508 return hv_vcpu->cpuid_cache.enlightenments_eax &
2509 HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED;
2510 case HVCALL_SEND_IPI_EX:
2511 if (!(hv_vcpu->cpuid_cache.enlightenments_eax &
2512 HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED))
2513 return false;
2514 fallthrough;
2515 case HVCALL_SEND_IPI:
2516 return hv_vcpu->cpuid_cache.enlightenments_eax &
2517 HV_X64_CLUSTER_IPI_RECOMMENDED;
2518 case HV_EXT_CALL_QUERY_CAPABILITIES ... HV_EXT_CALL_MAX:
2519 return hv_vcpu->cpuid_cache.features_ebx &
2520 HV_ENABLE_EXTENDED_HYPERCALLS;
2521 default:
2522 break;
2523 }
2524
2525 return true;
2526 }
2527
kvm_hv_hypercall(struct kvm_vcpu * vcpu)2528 int kvm_hv_hypercall(struct kvm_vcpu *vcpu)
2529 {
2530 struct kvm_vcpu_hv *hv_vcpu = to_hv_vcpu(vcpu);
2531 struct kvm_hv_hcall hc;
2532 u64 ret = HV_STATUS_SUCCESS;
2533
2534 /*
2535 * hypercall generates UD from non zero cpl and real mode
2536 * per HYPER-V spec
2537 */
2538 if (kvm_x86_call(get_cpl)(vcpu) != 0 || !is_protmode(vcpu)) {
2539 kvm_queue_exception(vcpu, UD_VECTOR);
2540 return 1;
2541 }
2542
2543 #ifdef CONFIG_X86_64
2544 if (is_64_bit_hypercall(vcpu)) {
2545 hc.param = kvm_rcx_read(vcpu);
2546 hc.ingpa = kvm_rdx_read(vcpu);
2547 hc.outgpa = kvm_r8_read(vcpu);
2548 } else
2549 #endif
2550 {
2551 hc.param = ((u64)kvm_rdx_read(vcpu) << 32) |
2552 (kvm_rax_read(vcpu) & 0xffffffff);
2553 hc.ingpa = ((u64)kvm_rbx_read(vcpu) << 32) |
2554 (kvm_rcx_read(vcpu) & 0xffffffff);
2555 hc.outgpa = ((u64)kvm_rdi_read(vcpu) << 32) |
2556 (kvm_rsi_read(vcpu) & 0xffffffff);
2557 }
2558
2559 hc.code = hc.param & 0xffff;
2560 hc.var_cnt = (hc.param & HV_HYPERCALL_VARHEAD_MASK) >> HV_HYPERCALL_VARHEAD_OFFSET;
2561 hc.fast = !!(hc.param & HV_HYPERCALL_FAST_BIT);
2562 hc.rep_cnt = (hc.param >> HV_HYPERCALL_REP_COMP_OFFSET) & 0xfff;
2563 hc.rep_idx = (hc.param >> HV_HYPERCALL_REP_START_OFFSET) & 0xfff;
2564 hc.rep = !!(hc.rep_cnt || hc.rep_idx);
2565
2566 trace_kvm_hv_hypercall(hc.code, hc.fast, hc.var_cnt, hc.rep_cnt,
2567 hc.rep_idx, hc.ingpa, hc.outgpa);
2568
2569 if (unlikely(!hv_check_hypercall_access(hv_vcpu, hc.code))) {
2570 ret = HV_STATUS_ACCESS_DENIED;
2571 goto hypercall_complete;
2572 }
2573
2574 if (unlikely(hc.param & HV_HYPERCALL_RSVD_MASK)) {
2575 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2576 goto hypercall_complete;
2577 }
2578
2579 if (hc.fast && is_xmm_fast_hypercall(&hc)) {
2580 if (unlikely(hv_vcpu->enforce_cpuid &&
2581 !(hv_vcpu->cpuid_cache.features_edx &
2582 HV_X64_HYPERCALL_XMM_INPUT_AVAILABLE))) {
2583 kvm_queue_exception(vcpu, UD_VECTOR);
2584 return 1;
2585 }
2586
2587 kvm_hv_hypercall_read_xmm(&hc);
2588 }
2589
2590 switch (hc.code) {
2591 case HVCALL_NOTIFY_LONG_SPIN_WAIT:
2592 if (unlikely(hc.rep || hc.var_cnt)) {
2593 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2594 break;
2595 }
2596 kvm_vcpu_on_spin(vcpu, true);
2597 break;
2598 case HVCALL_SIGNAL_EVENT:
2599 if (unlikely(hc.rep || hc.var_cnt)) {
2600 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2601 break;
2602 }
2603 ret = kvm_hvcall_signal_event(vcpu, &hc);
2604 if (ret != HV_STATUS_INVALID_PORT_ID)
2605 break;
2606 fallthrough; /* maybe userspace knows this conn_id */
2607 case HVCALL_POST_MESSAGE:
2608 /* don't bother userspace if it has no way to handle it */
2609 if (unlikely(hc.rep || hc.var_cnt || !to_hv_synic(vcpu)->active)) {
2610 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2611 break;
2612 }
2613 goto hypercall_userspace_exit;
2614 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST:
2615 if (unlikely(hc.var_cnt)) {
2616 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2617 break;
2618 }
2619 fallthrough;
2620 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX:
2621 if (unlikely(!hc.rep_cnt || hc.rep_idx)) {
2622 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2623 break;
2624 }
2625 ret = kvm_hv_flush_tlb(vcpu, &hc);
2626 break;
2627 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE:
2628 if (unlikely(hc.var_cnt)) {
2629 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2630 break;
2631 }
2632 fallthrough;
2633 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX:
2634 if (unlikely(hc.rep)) {
2635 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2636 break;
2637 }
2638 ret = kvm_hv_flush_tlb(vcpu, &hc);
2639 break;
2640 case HVCALL_SEND_IPI:
2641 if (unlikely(hc.var_cnt)) {
2642 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2643 break;
2644 }
2645 fallthrough;
2646 case HVCALL_SEND_IPI_EX:
2647 if (unlikely(hc.rep)) {
2648 ret = HV_STATUS_INVALID_HYPERCALL_INPUT;
2649 break;
2650 }
2651 ret = kvm_hv_send_ipi(vcpu, &hc);
2652 break;
2653 case HVCALL_POST_DEBUG_DATA:
2654 case HVCALL_RETRIEVE_DEBUG_DATA:
2655 if (unlikely(hc.fast)) {
2656 ret = HV_STATUS_INVALID_PARAMETER;
2657 break;
2658 }
2659 fallthrough;
2660 case HVCALL_RESET_DEBUG_SESSION: {
2661 struct kvm_hv_syndbg *syndbg = to_hv_syndbg(vcpu);
2662
2663 if (!kvm_hv_is_syndbg_enabled(vcpu)) {
2664 ret = HV_STATUS_INVALID_HYPERCALL_CODE;
2665 break;
2666 }
2667
2668 if (!(syndbg->options & HV_X64_SYNDBG_OPTION_USE_HCALLS)) {
2669 ret = HV_STATUS_OPERATION_DENIED;
2670 break;
2671 }
2672 goto hypercall_userspace_exit;
2673 }
2674 case HV_EXT_CALL_QUERY_CAPABILITIES ... HV_EXT_CALL_MAX:
2675 if (unlikely(hc.fast)) {
2676 ret = HV_STATUS_INVALID_PARAMETER;
2677 break;
2678 }
2679 goto hypercall_userspace_exit;
2680 default:
2681 ret = HV_STATUS_INVALID_HYPERCALL_CODE;
2682 break;
2683 }
2684
2685 hypercall_complete:
2686 return kvm_hv_hypercall_complete(vcpu, ret);
2687
2688 hypercall_userspace_exit:
2689 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
2690 vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL;
2691 vcpu->run->hyperv.u.hcall.input = hc.param;
2692 vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa;
2693 vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa;
2694 vcpu->arch.complete_userspace_io = kvm_hv_hypercall_complete_userspace;
2695 return 0;
2696 }
2697
kvm_hv_init_vm(struct kvm * kvm)2698 void kvm_hv_init_vm(struct kvm *kvm)
2699 {
2700 struct kvm_hv *hv = to_kvm_hv(kvm);
2701
2702 mutex_init(&hv->hv_lock);
2703 idr_init(&hv->conn_to_evt);
2704 }
2705
kvm_hv_destroy_vm(struct kvm * kvm)2706 void kvm_hv_destroy_vm(struct kvm *kvm)
2707 {
2708 struct kvm_hv *hv = to_kvm_hv(kvm);
2709 struct eventfd_ctx *eventfd;
2710 int i;
2711
2712 idr_for_each_entry(&hv->conn_to_evt, eventfd, i)
2713 eventfd_ctx_put(eventfd);
2714 idr_destroy(&hv->conn_to_evt);
2715 }
2716
kvm_hv_eventfd_assign(struct kvm * kvm,u32 conn_id,int fd)2717 static int kvm_hv_eventfd_assign(struct kvm *kvm, u32 conn_id, int fd)
2718 {
2719 struct kvm_hv *hv = to_kvm_hv(kvm);
2720 struct eventfd_ctx *eventfd;
2721 int ret;
2722
2723 eventfd = eventfd_ctx_fdget(fd);
2724 if (IS_ERR(eventfd))
2725 return PTR_ERR(eventfd);
2726
2727 mutex_lock(&hv->hv_lock);
2728 ret = idr_alloc(&hv->conn_to_evt, eventfd, conn_id, conn_id + 1,
2729 GFP_KERNEL_ACCOUNT);
2730 mutex_unlock(&hv->hv_lock);
2731
2732 if (ret >= 0)
2733 return 0;
2734
2735 if (ret == -ENOSPC)
2736 ret = -EEXIST;
2737 eventfd_ctx_put(eventfd);
2738 return ret;
2739 }
2740
kvm_hv_eventfd_deassign(struct kvm * kvm,u32 conn_id)2741 static int kvm_hv_eventfd_deassign(struct kvm *kvm, u32 conn_id)
2742 {
2743 struct kvm_hv *hv = to_kvm_hv(kvm);
2744 struct eventfd_ctx *eventfd;
2745
2746 mutex_lock(&hv->hv_lock);
2747 eventfd = idr_remove(&hv->conn_to_evt, conn_id);
2748 mutex_unlock(&hv->hv_lock);
2749
2750 if (!eventfd)
2751 return -ENOENT;
2752
2753 synchronize_srcu(&kvm->srcu);
2754 eventfd_ctx_put(eventfd);
2755 return 0;
2756 }
2757
kvm_vm_ioctl_hv_eventfd(struct kvm * kvm,struct kvm_hyperv_eventfd * args)2758 int kvm_vm_ioctl_hv_eventfd(struct kvm *kvm, struct kvm_hyperv_eventfd *args)
2759 {
2760 if ((args->flags & ~KVM_HYPERV_EVENTFD_DEASSIGN) ||
2761 (args->conn_id & ~KVM_HYPERV_CONN_ID_MASK))
2762 return -EINVAL;
2763
2764 if (args->flags == KVM_HYPERV_EVENTFD_DEASSIGN)
2765 return kvm_hv_eventfd_deassign(kvm, args->conn_id);
2766 return kvm_hv_eventfd_assign(kvm, args->conn_id, args->fd);
2767 }
2768
kvm_get_hv_cpuid(struct kvm_vcpu * vcpu,struct kvm_cpuid2 * cpuid,struct kvm_cpuid_entry2 __user * entries)2769 int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid,
2770 struct kvm_cpuid_entry2 __user *entries)
2771 {
2772 uint16_t evmcs_ver = 0;
2773 struct kvm_cpuid_entry2 cpuid_entries[] = {
2774 { .function = HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS },
2775 { .function = HYPERV_CPUID_INTERFACE },
2776 { .function = HYPERV_CPUID_VERSION },
2777 { .function = HYPERV_CPUID_FEATURES },
2778 { .function = HYPERV_CPUID_ENLIGHTMENT_INFO },
2779 { .function = HYPERV_CPUID_IMPLEMENT_LIMITS },
2780 { .function = HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS },
2781 { .function = HYPERV_CPUID_SYNDBG_INTERFACE },
2782 { .function = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES },
2783 { .function = HYPERV_CPUID_NESTED_FEATURES },
2784 };
2785 int i, nent = ARRAY_SIZE(cpuid_entries);
2786
2787 if (kvm_x86_ops.nested_ops->get_evmcs_version)
2788 evmcs_ver = kvm_x86_ops.nested_ops->get_evmcs_version(vcpu);
2789
2790 if (cpuid->nent < nent)
2791 return -E2BIG;
2792
2793 if (cpuid->nent > nent)
2794 cpuid->nent = nent;
2795
2796 for (i = 0; i < nent; i++) {
2797 struct kvm_cpuid_entry2 *ent = &cpuid_entries[i];
2798 u32 signature[3];
2799
2800 switch (ent->function) {
2801 case HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS:
2802 memcpy(signature, "Linux KVM Hv", 12);
2803
2804 ent->eax = HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES;
2805 ent->ebx = signature[0];
2806 ent->ecx = signature[1];
2807 ent->edx = signature[2];
2808 break;
2809
2810 case HYPERV_CPUID_INTERFACE:
2811 ent->eax = HYPERV_CPUID_SIGNATURE_EAX;
2812 break;
2813
2814 case HYPERV_CPUID_VERSION:
2815 /*
2816 * We implement some Hyper-V 2016 functions so let's use
2817 * this version.
2818 */
2819 ent->eax = 0x00003839;
2820 ent->ebx = 0x000A0000;
2821 break;
2822
2823 case HYPERV_CPUID_FEATURES:
2824 ent->eax |= HV_MSR_VP_RUNTIME_AVAILABLE;
2825 ent->eax |= HV_MSR_TIME_REF_COUNT_AVAILABLE;
2826 ent->eax |= HV_MSR_SYNIC_AVAILABLE;
2827 ent->eax |= HV_MSR_SYNTIMER_AVAILABLE;
2828 ent->eax |= HV_MSR_APIC_ACCESS_AVAILABLE;
2829 ent->eax |= HV_MSR_HYPERCALL_AVAILABLE;
2830 ent->eax |= HV_MSR_VP_INDEX_AVAILABLE;
2831 ent->eax |= HV_MSR_RESET_AVAILABLE;
2832 ent->eax |= HV_MSR_REFERENCE_TSC_AVAILABLE;
2833 ent->eax |= HV_ACCESS_FREQUENCY_MSRS;
2834 ent->eax |= HV_ACCESS_REENLIGHTENMENT;
2835 ent->eax |= HV_ACCESS_TSC_INVARIANT;
2836
2837 ent->ebx |= HV_POST_MESSAGES;
2838 ent->ebx |= HV_SIGNAL_EVENTS;
2839 ent->ebx |= HV_ENABLE_EXTENDED_HYPERCALLS;
2840
2841 ent->edx |= HV_X64_HYPERCALL_XMM_INPUT_AVAILABLE;
2842 ent->edx |= HV_FEATURE_FREQUENCY_MSRS_AVAILABLE;
2843 ent->edx |= HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE;
2844
2845 ent->ebx |= HV_DEBUGGING;
2846 ent->edx |= HV_X64_GUEST_DEBUGGING_AVAILABLE;
2847 ent->edx |= HV_FEATURE_DEBUG_MSRS_AVAILABLE;
2848 ent->edx |= HV_FEATURE_EXT_GVA_RANGES_FLUSH;
2849
2850 /*
2851 * Direct Synthetic timers only make sense with in-kernel
2852 * LAPIC
2853 */
2854 if (!vcpu || lapic_in_kernel(vcpu))
2855 ent->edx |= HV_STIMER_DIRECT_MODE_AVAILABLE;
2856
2857 break;
2858
2859 case HYPERV_CPUID_ENLIGHTMENT_INFO:
2860 ent->eax |= HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED;
2861 ent->eax |= HV_X64_APIC_ACCESS_RECOMMENDED;
2862 ent->eax |= HV_X64_RELAXED_TIMING_RECOMMENDED;
2863 if (!vcpu || lapic_in_kernel(vcpu))
2864 ent->eax |= HV_X64_CLUSTER_IPI_RECOMMENDED;
2865 ent->eax |= HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED;
2866 if (evmcs_ver)
2867 ent->eax |= HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
2868 if (!cpu_smt_possible())
2869 ent->eax |= HV_X64_NO_NONARCH_CORESHARING;
2870
2871 ent->eax |= HV_DEPRECATING_AEOI_RECOMMENDED;
2872 /*
2873 * Default number of spinlock retry attempts, matches
2874 * HyperV 2016.
2875 */
2876 ent->ebx = 0x00000FFF;
2877
2878 break;
2879
2880 case HYPERV_CPUID_IMPLEMENT_LIMITS:
2881 /* Maximum number of virtual processors */
2882 ent->eax = KVM_MAX_VCPUS;
2883 /*
2884 * Maximum number of logical processors, matches
2885 * HyperV 2016.
2886 */
2887 ent->ebx = 64;
2888
2889 break;
2890
2891 case HYPERV_CPUID_NESTED_FEATURES:
2892 ent->eax = evmcs_ver;
2893 ent->eax |= HV_X64_NESTED_DIRECT_FLUSH;
2894 ent->eax |= HV_X64_NESTED_MSR_BITMAP;
2895 ent->ebx |= HV_X64_NESTED_EVMCS1_PERF_GLOBAL_CTRL;
2896 break;
2897
2898 case HYPERV_CPUID_SYNDBG_VENDOR_AND_MAX_FUNCTIONS:
2899 memcpy(signature, "Linux KVM Hv", 12);
2900
2901 ent->eax = 0;
2902 ent->ebx = signature[0];
2903 ent->ecx = signature[1];
2904 ent->edx = signature[2];
2905 break;
2906
2907 case HYPERV_CPUID_SYNDBG_INTERFACE:
2908 memcpy(signature, "VS#1\0\0\0\0\0\0\0\0", 12);
2909 ent->eax = signature[0];
2910 break;
2911
2912 case HYPERV_CPUID_SYNDBG_PLATFORM_CAPABILITIES:
2913 ent->eax |= HV_X64_SYNDBG_CAP_ALLOW_KERNEL_DEBUGGING;
2914 break;
2915
2916 default:
2917 break;
2918 }
2919 }
2920
2921 if (copy_to_user(entries, cpuid_entries,
2922 nent * sizeof(struct kvm_cpuid_entry2)))
2923 return -EFAULT;
2924
2925 return 0;
2926 }
2927