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