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