2 * KVM Microsoft Hyper-V emulation
4 * derived from arch/x86/kvm/x86.c
6 * Copyright (C) 2006 Qumranet, Inc.
7 * Copyright (C) 2008 Qumranet, Inc.
8 * Copyright IBM Corporation, 2008
9 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
10 * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com>
13 * Avi Kivity <avi@qumranet.com>
14 * Yaniv Kamay <yaniv@qumranet.com>
15 * Amit Shah <amit.shah@qumranet.com>
16 * Ben-Ami Yassour <benami@il.ibm.com>
17 * Andrey Smetanin <asmetanin@virtuozzo.com>
19 * This work is licensed under the terms of the GNU GPL, version 2. See
20 * the COPYING file in the top-level directory.
29 #include <linux/kvm_host.h>
30 #include <linux/highmem.h>
31 #include <linux/sched/cputime.h>
32 #include <linux/eventfd.h>
34 #include <asm/apicdef.h>
35 #include <trace/events/kvm.h>
39 static inline u64
synic_read_sint(struct kvm_vcpu_hv_synic
*synic
, int sint
)
41 return atomic64_read(&synic
->sint
[sint
]);
44 static inline int synic_get_sint_vector(u64 sint_value
)
46 if (sint_value
& HV_SYNIC_SINT_MASKED
)
48 return sint_value
& HV_SYNIC_SINT_VECTOR_MASK
;
51 static bool synic_has_vector_connected(struct kvm_vcpu_hv_synic
*synic
,
56 for (i
= 0; i
< ARRAY_SIZE(synic
->sint
); i
++) {
57 if (synic_get_sint_vector(synic_read_sint(synic
, i
)) == vector
)
63 static bool synic_has_vector_auto_eoi(struct kvm_vcpu_hv_synic
*synic
,
69 for (i
= 0; i
< ARRAY_SIZE(synic
->sint
); i
++) {
70 sint_value
= synic_read_sint(synic
, i
);
71 if (synic_get_sint_vector(sint_value
) == vector
&&
72 sint_value
& HV_SYNIC_SINT_AUTO_EOI
)
78 static void synic_update_vector(struct kvm_vcpu_hv_synic
*synic
,
81 if (vector
< HV_SYNIC_FIRST_VALID_VECTOR
)
84 if (synic_has_vector_connected(synic
, vector
))
85 __set_bit(vector
, synic
->vec_bitmap
);
87 __clear_bit(vector
, synic
->vec_bitmap
);
89 if (synic_has_vector_auto_eoi(synic
, vector
))
90 __set_bit(vector
, synic
->auto_eoi_bitmap
);
92 __clear_bit(vector
, synic
->auto_eoi_bitmap
);
95 static int synic_set_sint(struct kvm_vcpu_hv_synic
*synic
, int sint
,
98 int vector
, old_vector
;
101 vector
= data
& HV_SYNIC_SINT_VECTOR_MASK
;
102 masked
= data
& HV_SYNIC_SINT_MASKED
;
105 * Valid vectors are 16-255, however, nested Hyper-V attempts to write
106 * default '0x10000' value on boot and this should not #GP. We need to
107 * allow zero-initing the register from host as well.
109 if (vector
< HV_SYNIC_FIRST_VALID_VECTOR
&& !host
&& !masked
)
112 * Guest may configure multiple SINTs to use the same vector, so
113 * we maintain a bitmap of vectors handled by synic, and a
114 * bitmap of vectors with auto-eoi behavior. The bitmaps are
115 * updated here, and atomically queried on fast paths.
117 old_vector
= synic_read_sint(synic
, sint
) & HV_SYNIC_SINT_VECTOR_MASK
;
119 atomic64_set(&synic
->sint
[sint
], data
);
121 synic_update_vector(synic
, old_vector
);
123 synic_update_vector(synic
, vector
);
125 /* Load SynIC vectors into EOI exit bitmap */
126 kvm_make_request(KVM_REQ_SCAN_IOAPIC
, synic_to_vcpu(synic
));
130 static struct kvm_vcpu
*get_vcpu_by_vpidx(struct kvm
*kvm
, u32 vpidx
)
132 struct kvm_vcpu
*vcpu
= NULL
;
135 if (vpidx
< KVM_MAX_VCPUS
)
136 vcpu
= kvm_get_vcpu(kvm
, vpidx
);
137 if (vcpu
&& vcpu_to_hv_vcpu(vcpu
)->vp_index
== vpidx
)
139 kvm_for_each_vcpu(i
, vcpu
, kvm
)
140 if (vcpu_to_hv_vcpu(vcpu
)->vp_index
== vpidx
)
145 static struct kvm_vcpu_hv_synic
*synic_get(struct kvm
*kvm
, u32 vpidx
)
147 struct kvm_vcpu
*vcpu
;
148 struct kvm_vcpu_hv_synic
*synic
;
150 vcpu
= get_vcpu_by_vpidx(kvm
, vpidx
);
153 synic
= vcpu_to_synic(vcpu
);
154 return (synic
->active
) ? synic
: NULL
;
157 static void synic_clear_sint_msg_pending(struct kvm_vcpu_hv_synic
*synic
,
160 struct kvm_vcpu
*vcpu
= synic_to_vcpu(synic
);
163 struct hv_message
*msg
;
164 struct hv_message_page
*msg_page
;
166 gpa
= synic
->msg_page
& PAGE_MASK
;
167 page
= kvm_vcpu_gfn_to_page(vcpu
, gpa
>> PAGE_SHIFT
);
168 if (is_error_page(page
)) {
169 vcpu_err(vcpu
, "Hyper-V SynIC can't get msg page, gpa 0x%llx\n",
173 msg_page
= kmap_atomic(page
);
175 msg
= &msg_page
->sint_message
[sint
];
176 msg
->header
.message_flags
.msg_pending
= 0;
178 kunmap_atomic(msg_page
);
179 kvm_release_page_dirty(page
);
180 kvm_vcpu_mark_page_dirty(vcpu
, gpa
>> PAGE_SHIFT
);
183 static void kvm_hv_notify_acked_sint(struct kvm_vcpu
*vcpu
, u32 sint
)
185 struct kvm
*kvm
= vcpu
->kvm
;
186 struct kvm_vcpu_hv_synic
*synic
= vcpu_to_synic(vcpu
);
187 struct kvm_vcpu_hv
*hv_vcpu
= vcpu_to_hv_vcpu(vcpu
);
188 struct kvm_vcpu_hv_stimer
*stimer
;
189 int gsi
, idx
, stimers_pending
;
191 trace_kvm_hv_notify_acked_sint(vcpu
->vcpu_id
, sint
);
193 if (synic
->msg_page
& HV_SYNIC_SIMP_ENABLE
)
194 synic_clear_sint_msg_pending(synic
, sint
);
196 /* Try to deliver pending Hyper-V SynIC timers messages */
198 for (idx
= 0; idx
< ARRAY_SIZE(hv_vcpu
->stimer
); idx
++) {
199 stimer
= &hv_vcpu
->stimer
[idx
];
200 if (stimer
->msg_pending
&&
201 (stimer
->config
& HV_STIMER_ENABLE
) &&
202 HV_STIMER_SINT(stimer
->config
) == sint
) {
203 set_bit(stimer
->index
,
204 hv_vcpu
->stimer_pending_bitmap
);
209 kvm_make_request(KVM_REQ_HV_STIMER
, vcpu
);
211 idx
= srcu_read_lock(&kvm
->irq_srcu
);
212 gsi
= atomic_read(&synic
->sint_to_gsi
[sint
]);
214 kvm_notify_acked_gsi(kvm
, gsi
);
215 srcu_read_unlock(&kvm
->irq_srcu
, idx
);
218 static void synic_exit(struct kvm_vcpu_hv_synic
*synic
, u32 msr
)
220 struct kvm_vcpu
*vcpu
= synic_to_vcpu(synic
);
221 struct kvm_vcpu_hv
*hv_vcpu
= &vcpu
->arch
.hyperv
;
223 hv_vcpu
->exit
.type
= KVM_EXIT_HYPERV_SYNIC
;
224 hv_vcpu
->exit
.u
.synic
.msr
= msr
;
225 hv_vcpu
->exit
.u
.synic
.control
= synic
->control
;
226 hv_vcpu
->exit
.u
.synic
.evt_page
= synic
->evt_page
;
227 hv_vcpu
->exit
.u
.synic
.msg_page
= synic
->msg_page
;
229 kvm_make_request(KVM_REQ_HV_EXIT
, vcpu
);
232 static int synic_set_msr(struct kvm_vcpu_hv_synic
*synic
,
233 u32 msr
, u64 data
, bool host
)
235 struct kvm_vcpu
*vcpu
= synic_to_vcpu(synic
);
238 if (!synic
->active
&& !host
)
241 trace_kvm_hv_synic_set_msr(vcpu
->vcpu_id
, msr
, data
, host
);
245 case HV_X64_MSR_SCONTROL
:
246 synic
->control
= data
;
248 synic_exit(synic
, msr
);
250 case HV_X64_MSR_SVERSION
:
255 synic
->version
= data
;
257 case HV_X64_MSR_SIEFP
:
258 if ((data
& HV_SYNIC_SIEFP_ENABLE
) && !host
&&
259 !synic
->dont_zero_synic_pages
)
260 if (kvm_clear_guest(vcpu
->kvm
,
261 data
& PAGE_MASK
, PAGE_SIZE
)) {
265 synic
->evt_page
= data
;
267 synic_exit(synic
, msr
);
269 case HV_X64_MSR_SIMP
:
270 if ((data
& HV_SYNIC_SIMP_ENABLE
) && !host
&&
271 !synic
->dont_zero_synic_pages
)
272 if (kvm_clear_guest(vcpu
->kvm
,
273 data
& PAGE_MASK
, PAGE_SIZE
)) {
277 synic
->msg_page
= data
;
279 synic_exit(synic
, msr
);
281 case HV_X64_MSR_EOM
: {
284 for (i
= 0; i
< ARRAY_SIZE(synic
->sint
); i
++)
285 kvm_hv_notify_acked_sint(vcpu
, i
);
288 case HV_X64_MSR_SINT0
... HV_X64_MSR_SINT15
:
289 ret
= synic_set_sint(synic
, msr
- HV_X64_MSR_SINT0
, data
, host
);
298 static int synic_get_msr(struct kvm_vcpu_hv_synic
*synic
, u32 msr
, u64
*pdata
,
303 if (!synic
->active
&& !host
)
308 case HV_X64_MSR_SCONTROL
:
309 *pdata
= synic
->control
;
311 case HV_X64_MSR_SVERSION
:
312 *pdata
= synic
->version
;
314 case HV_X64_MSR_SIEFP
:
315 *pdata
= synic
->evt_page
;
317 case HV_X64_MSR_SIMP
:
318 *pdata
= synic
->msg_page
;
323 case HV_X64_MSR_SINT0
... HV_X64_MSR_SINT15
:
324 *pdata
= atomic64_read(&synic
->sint
[msr
- HV_X64_MSR_SINT0
]);
333 static int synic_set_irq(struct kvm_vcpu_hv_synic
*synic
, u32 sint
)
335 struct kvm_vcpu
*vcpu
= synic_to_vcpu(synic
);
336 struct kvm_lapic_irq irq
;
339 if (sint
>= ARRAY_SIZE(synic
->sint
))
342 vector
= synic_get_sint_vector(synic_read_sint(synic
, sint
));
346 memset(&irq
, 0, sizeof(irq
));
347 irq
.shorthand
= APIC_DEST_SELF
;
348 irq
.dest_mode
= APIC_DEST_PHYSICAL
;
349 irq
.delivery_mode
= APIC_DM_FIXED
;
353 ret
= kvm_irq_delivery_to_apic(vcpu
->kvm
, vcpu
->arch
.apic
, &irq
, NULL
);
354 trace_kvm_hv_synic_set_irq(vcpu
->vcpu_id
, sint
, irq
.vector
, ret
);
358 int kvm_hv_synic_set_irq(struct kvm
*kvm
, u32 vpidx
, u32 sint
)
360 struct kvm_vcpu_hv_synic
*synic
;
362 synic
= synic_get(kvm
, vpidx
);
366 return synic_set_irq(synic
, sint
);
369 void kvm_hv_synic_send_eoi(struct kvm_vcpu
*vcpu
, int vector
)
371 struct kvm_vcpu_hv_synic
*synic
= vcpu_to_synic(vcpu
);
374 trace_kvm_hv_synic_send_eoi(vcpu
->vcpu_id
, vector
);
376 for (i
= 0; i
< ARRAY_SIZE(synic
->sint
); i
++)
377 if (synic_get_sint_vector(synic_read_sint(synic
, i
)) == vector
)
378 kvm_hv_notify_acked_sint(vcpu
, i
);
381 static int kvm_hv_set_sint_gsi(struct kvm
*kvm
, u32 vpidx
, u32 sint
, int gsi
)
383 struct kvm_vcpu_hv_synic
*synic
;
385 synic
= synic_get(kvm
, vpidx
);
389 if (sint
>= ARRAY_SIZE(synic
->sint_to_gsi
))
392 atomic_set(&synic
->sint_to_gsi
[sint
], gsi
);
396 void kvm_hv_irq_routing_update(struct kvm
*kvm
)
398 struct kvm_irq_routing_table
*irq_rt
;
399 struct kvm_kernel_irq_routing_entry
*e
;
402 irq_rt
= srcu_dereference_check(kvm
->irq_routing
, &kvm
->irq_srcu
,
403 lockdep_is_held(&kvm
->irq_lock
));
405 for (gsi
= 0; gsi
< irq_rt
->nr_rt_entries
; gsi
++) {
406 hlist_for_each_entry(e
, &irq_rt
->map
[gsi
], link
) {
407 if (e
->type
== KVM_IRQ_ROUTING_HV_SINT
)
408 kvm_hv_set_sint_gsi(kvm
, e
->hv_sint
.vcpu
,
409 e
->hv_sint
.sint
, gsi
);
414 static void synic_init(struct kvm_vcpu_hv_synic
*synic
)
418 memset(synic
, 0, sizeof(*synic
));
419 synic
->version
= HV_SYNIC_VERSION_1
;
420 for (i
= 0; i
< ARRAY_SIZE(synic
->sint
); i
++) {
421 atomic64_set(&synic
->sint
[i
], HV_SYNIC_SINT_MASKED
);
422 atomic_set(&synic
->sint_to_gsi
[i
], -1);
426 static u64
get_time_ref_counter(struct kvm
*kvm
)
428 struct kvm_hv
*hv
= &kvm
->arch
.hyperv
;
429 struct kvm_vcpu
*vcpu
;
433 * The guest has not set up the TSC page or the clock isn't
434 * stable, fall back to get_kvmclock_ns.
436 if (!hv
->tsc_ref
.tsc_sequence
)
437 return div_u64(get_kvmclock_ns(kvm
), 100);
439 vcpu
= kvm_get_vcpu(kvm
, 0);
440 tsc
= kvm_read_l1_tsc(vcpu
, rdtsc());
441 return mul_u64_u64_shr(tsc
, hv
->tsc_ref
.tsc_scale
, 64)
442 + hv
->tsc_ref
.tsc_offset
;
445 static void stimer_mark_pending(struct kvm_vcpu_hv_stimer
*stimer
,
448 struct kvm_vcpu
*vcpu
= stimer_to_vcpu(stimer
);
450 set_bit(stimer
->index
,
451 vcpu_to_hv_vcpu(vcpu
)->stimer_pending_bitmap
);
452 kvm_make_request(KVM_REQ_HV_STIMER
, vcpu
);
457 static void stimer_cleanup(struct kvm_vcpu_hv_stimer
*stimer
)
459 struct kvm_vcpu
*vcpu
= stimer_to_vcpu(stimer
);
461 trace_kvm_hv_stimer_cleanup(stimer_to_vcpu(stimer
)->vcpu_id
,
464 hrtimer_cancel(&stimer
->timer
);
465 clear_bit(stimer
->index
,
466 vcpu_to_hv_vcpu(vcpu
)->stimer_pending_bitmap
);
467 stimer
->msg_pending
= false;
468 stimer
->exp_time
= 0;
471 static enum hrtimer_restart
stimer_timer_callback(struct hrtimer
*timer
)
473 struct kvm_vcpu_hv_stimer
*stimer
;
475 stimer
= container_of(timer
, struct kvm_vcpu_hv_stimer
, timer
);
476 trace_kvm_hv_stimer_callback(stimer_to_vcpu(stimer
)->vcpu_id
,
478 stimer_mark_pending(stimer
, true);
480 return HRTIMER_NORESTART
;
484 * stimer_start() assumptions:
485 * a) stimer->count is not equal to 0
486 * b) stimer->config has HV_STIMER_ENABLE flag
488 static int stimer_start(struct kvm_vcpu_hv_stimer
*stimer
)
493 time_now
= get_time_ref_counter(stimer_to_vcpu(stimer
)->kvm
);
494 ktime_now
= ktime_get();
496 if (stimer
->config
& HV_STIMER_PERIODIC
) {
497 if (stimer
->exp_time
) {
498 if (time_now
>= stimer
->exp_time
) {
501 div64_u64_rem(time_now
- stimer
->exp_time
,
502 stimer
->count
, &remainder
);
504 time_now
+ (stimer
->count
- remainder
);
507 stimer
->exp_time
= time_now
+ stimer
->count
;
509 trace_kvm_hv_stimer_start_periodic(
510 stimer_to_vcpu(stimer
)->vcpu_id
,
512 time_now
, stimer
->exp_time
);
514 hrtimer_start(&stimer
->timer
,
515 ktime_add_ns(ktime_now
,
516 100 * (stimer
->exp_time
- time_now
)),
520 stimer
->exp_time
= stimer
->count
;
521 if (time_now
>= stimer
->count
) {
523 * Expire timer according to Hypervisor Top-Level Functional
524 * specification v4(15.3.1):
525 * "If a one shot is enabled and the specified count is in
526 * the past, it will expire immediately."
528 stimer_mark_pending(stimer
, false);
532 trace_kvm_hv_stimer_start_one_shot(stimer_to_vcpu(stimer
)->vcpu_id
,
534 time_now
, stimer
->count
);
536 hrtimer_start(&stimer
->timer
,
537 ktime_add_ns(ktime_now
, 100 * (stimer
->count
- time_now
)),
542 static int stimer_set_config(struct kvm_vcpu_hv_stimer
*stimer
, u64 config
,
545 trace_kvm_hv_stimer_set_config(stimer_to_vcpu(stimer
)->vcpu_id
,
546 stimer
->index
, config
, host
);
548 stimer_cleanup(stimer
);
549 if ((stimer
->config
& HV_STIMER_ENABLE
) && HV_STIMER_SINT(config
) == 0)
550 config
&= ~HV_STIMER_ENABLE
;
551 stimer
->config
= config
;
552 stimer_mark_pending(stimer
, false);
556 static int stimer_set_count(struct kvm_vcpu_hv_stimer
*stimer
, u64 count
,
559 trace_kvm_hv_stimer_set_count(stimer_to_vcpu(stimer
)->vcpu_id
,
560 stimer
->index
, count
, host
);
562 stimer_cleanup(stimer
);
563 stimer
->count
= count
;
564 if (stimer
->count
== 0)
565 stimer
->config
&= ~HV_STIMER_ENABLE
;
566 else if (stimer
->config
& HV_STIMER_AUTOENABLE
)
567 stimer
->config
|= HV_STIMER_ENABLE
;
568 stimer_mark_pending(stimer
, false);
572 static int stimer_get_config(struct kvm_vcpu_hv_stimer
*stimer
, u64
*pconfig
)
574 *pconfig
= stimer
->config
;
578 static int stimer_get_count(struct kvm_vcpu_hv_stimer
*stimer
, u64
*pcount
)
580 *pcount
= stimer
->count
;
584 static int synic_deliver_msg(struct kvm_vcpu_hv_synic
*synic
, u32 sint
,
585 struct hv_message
*src_msg
)
587 struct kvm_vcpu
*vcpu
= synic_to_vcpu(synic
);
590 struct hv_message
*dst_msg
;
592 struct hv_message_page
*msg_page
;
594 if (!(synic
->msg_page
& HV_SYNIC_SIMP_ENABLE
))
597 gpa
= synic
->msg_page
& PAGE_MASK
;
598 page
= kvm_vcpu_gfn_to_page(vcpu
, gpa
>> PAGE_SHIFT
);
599 if (is_error_page(page
))
602 msg_page
= kmap_atomic(page
);
603 dst_msg
= &msg_page
->sint_message
[sint
];
604 if (sync_cmpxchg(&dst_msg
->header
.message_type
, HVMSG_NONE
,
605 src_msg
->header
.message_type
) != HVMSG_NONE
) {
606 dst_msg
->header
.message_flags
.msg_pending
= 1;
609 memcpy(&dst_msg
->u
.payload
, &src_msg
->u
.payload
,
610 src_msg
->header
.payload_size
);
611 dst_msg
->header
.message_type
= src_msg
->header
.message_type
;
612 dst_msg
->header
.payload_size
= src_msg
->header
.payload_size
;
613 r
= synic_set_irq(synic
, sint
);
619 kunmap_atomic(msg_page
);
620 kvm_release_page_dirty(page
);
621 kvm_vcpu_mark_page_dirty(vcpu
, gpa
>> PAGE_SHIFT
);
625 static int stimer_send_msg(struct kvm_vcpu_hv_stimer
*stimer
)
627 struct kvm_vcpu
*vcpu
= stimer_to_vcpu(stimer
);
628 struct hv_message
*msg
= &stimer
->msg
;
629 struct hv_timer_message_payload
*payload
=
630 (struct hv_timer_message_payload
*)&msg
->u
.payload
;
632 payload
->expiration_time
= stimer
->exp_time
;
633 payload
->delivery_time
= get_time_ref_counter(vcpu
->kvm
);
634 return synic_deliver_msg(vcpu_to_synic(vcpu
),
635 HV_STIMER_SINT(stimer
->config
), msg
);
638 static void stimer_expiration(struct kvm_vcpu_hv_stimer
*stimer
)
642 stimer
->msg_pending
= true;
643 r
= stimer_send_msg(stimer
);
644 trace_kvm_hv_stimer_expiration(stimer_to_vcpu(stimer
)->vcpu_id
,
647 stimer
->msg_pending
= false;
648 if (!(stimer
->config
& HV_STIMER_PERIODIC
))
649 stimer
->config
&= ~HV_STIMER_ENABLE
;
653 void kvm_hv_process_stimers(struct kvm_vcpu
*vcpu
)
655 struct kvm_vcpu_hv
*hv_vcpu
= vcpu_to_hv_vcpu(vcpu
);
656 struct kvm_vcpu_hv_stimer
*stimer
;
657 u64 time_now
, exp_time
;
660 for (i
= 0; i
< ARRAY_SIZE(hv_vcpu
->stimer
); i
++)
661 if (test_and_clear_bit(i
, hv_vcpu
->stimer_pending_bitmap
)) {
662 stimer
= &hv_vcpu
->stimer
[i
];
663 if (stimer
->config
& HV_STIMER_ENABLE
) {
664 exp_time
= stimer
->exp_time
;
668 get_time_ref_counter(vcpu
->kvm
);
669 if (time_now
>= exp_time
)
670 stimer_expiration(stimer
);
673 if ((stimer
->config
& HV_STIMER_ENABLE
) &&
675 if (!stimer
->msg_pending
)
676 stimer_start(stimer
);
678 stimer_cleanup(stimer
);
683 void kvm_hv_vcpu_uninit(struct kvm_vcpu
*vcpu
)
685 struct kvm_vcpu_hv
*hv_vcpu
= vcpu_to_hv_vcpu(vcpu
);
688 for (i
= 0; i
< ARRAY_SIZE(hv_vcpu
->stimer
); i
++)
689 stimer_cleanup(&hv_vcpu
->stimer
[i
]);
692 static void stimer_prepare_msg(struct kvm_vcpu_hv_stimer
*stimer
)
694 struct hv_message
*msg
= &stimer
->msg
;
695 struct hv_timer_message_payload
*payload
=
696 (struct hv_timer_message_payload
*)&msg
->u
.payload
;
698 memset(&msg
->header
, 0, sizeof(msg
->header
));
699 msg
->header
.message_type
= HVMSG_TIMER_EXPIRED
;
700 msg
->header
.payload_size
= sizeof(*payload
);
702 payload
->timer_index
= stimer
->index
;
703 payload
->expiration_time
= 0;
704 payload
->delivery_time
= 0;
707 static void stimer_init(struct kvm_vcpu_hv_stimer
*stimer
, int timer_index
)
709 memset(stimer
, 0, sizeof(*stimer
));
710 stimer
->index
= timer_index
;
711 hrtimer_init(&stimer
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
712 stimer
->timer
.function
= stimer_timer_callback
;
713 stimer_prepare_msg(stimer
);
716 void kvm_hv_vcpu_init(struct kvm_vcpu
*vcpu
)
718 struct kvm_vcpu_hv
*hv_vcpu
= vcpu_to_hv_vcpu(vcpu
);
721 synic_init(&hv_vcpu
->synic
);
723 bitmap_zero(hv_vcpu
->stimer_pending_bitmap
, HV_SYNIC_STIMER_COUNT
);
724 for (i
= 0; i
< ARRAY_SIZE(hv_vcpu
->stimer
); i
++)
725 stimer_init(&hv_vcpu
->stimer
[i
], i
);
728 void kvm_hv_vcpu_postcreate(struct kvm_vcpu
*vcpu
)
730 struct kvm_vcpu_hv
*hv_vcpu
= vcpu_to_hv_vcpu(vcpu
);
732 hv_vcpu
->vp_index
= kvm_vcpu_get_idx(vcpu
);
735 int kvm_hv_activate_synic(struct kvm_vcpu
*vcpu
, bool dont_zero_synic_pages
)
737 struct kvm_vcpu_hv_synic
*synic
= vcpu_to_synic(vcpu
);
740 * Hyper-V SynIC auto EOI SINT's are
741 * not compatible with APICV, so deactivate APICV
743 kvm_vcpu_deactivate_apicv(vcpu
);
744 synic
->active
= true;
745 synic
->dont_zero_synic_pages
= dont_zero_synic_pages
;
749 static bool kvm_hv_msr_partition_wide(u32 msr
)
754 case HV_X64_MSR_GUEST_OS_ID
:
755 case HV_X64_MSR_HYPERCALL
:
756 case HV_X64_MSR_REFERENCE_TSC
:
757 case HV_X64_MSR_TIME_REF_COUNT
:
758 case HV_X64_MSR_CRASH_CTL
:
759 case HV_X64_MSR_CRASH_P0
... HV_X64_MSR_CRASH_P4
:
760 case HV_X64_MSR_RESET
:
761 case HV_X64_MSR_REENLIGHTENMENT_CONTROL
:
762 case HV_X64_MSR_TSC_EMULATION_CONTROL
:
763 case HV_X64_MSR_TSC_EMULATION_STATUS
:
771 static int kvm_hv_msr_get_crash_data(struct kvm_vcpu
*vcpu
,
772 u32 index
, u64
*pdata
)
774 struct kvm_hv
*hv
= &vcpu
->kvm
->arch
.hyperv
;
776 if (WARN_ON_ONCE(index
>= ARRAY_SIZE(hv
->hv_crash_param
)))
779 *pdata
= hv
->hv_crash_param
[index
];
783 static int kvm_hv_msr_get_crash_ctl(struct kvm_vcpu
*vcpu
, u64
*pdata
)
785 struct kvm_hv
*hv
= &vcpu
->kvm
->arch
.hyperv
;
787 *pdata
= hv
->hv_crash_ctl
;
791 static int kvm_hv_msr_set_crash_ctl(struct kvm_vcpu
*vcpu
, u64 data
, bool host
)
793 struct kvm_hv
*hv
= &vcpu
->kvm
->arch
.hyperv
;
796 hv
->hv_crash_ctl
= data
& HV_X64_MSR_CRASH_CTL_NOTIFY
;
798 if (!host
&& (data
& HV_X64_MSR_CRASH_CTL_NOTIFY
)) {
800 vcpu_debug(vcpu
, "hv crash (0x%llx 0x%llx 0x%llx 0x%llx 0x%llx)\n",
801 hv
->hv_crash_param
[0],
802 hv
->hv_crash_param
[1],
803 hv
->hv_crash_param
[2],
804 hv
->hv_crash_param
[3],
805 hv
->hv_crash_param
[4]);
807 /* Send notification about crash to user space */
808 kvm_make_request(KVM_REQ_HV_CRASH
, vcpu
);
814 static int kvm_hv_msr_set_crash_data(struct kvm_vcpu
*vcpu
,
817 struct kvm_hv
*hv
= &vcpu
->kvm
->arch
.hyperv
;
819 if (WARN_ON_ONCE(index
>= ARRAY_SIZE(hv
->hv_crash_param
)))
822 hv
->hv_crash_param
[index
] = data
;
827 * The kvmclock and Hyper-V TSC page use similar formulas, and converting
828 * between them is possible:
831 * nsec = (ticks - tsc_timestamp) * tsc_to_system_mul * 2^(tsc_shift-32)
835 * nsec/100 = ticks * scale / 2^64 + offset
837 * When tsc_timestamp = system_time = 0, offset is zero in the Hyper-V formula.
838 * By dividing the kvmclock formula by 100 and equating what's left we get:
839 * ticks * scale / 2^64 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
840 * scale / 2^64 = tsc_to_system_mul * 2^(tsc_shift-32) / 100
841 * scale = tsc_to_system_mul * 2^(32+tsc_shift) / 100
843 * Now expand the kvmclock formula and divide by 100:
844 * nsec = ticks * tsc_to_system_mul * 2^(tsc_shift-32)
845 * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32)
847 * nsec/100 = ticks * tsc_to_system_mul * 2^(tsc_shift-32) / 100
848 * - tsc_timestamp * tsc_to_system_mul * 2^(tsc_shift-32) / 100
849 * + system_time / 100
851 * Replace tsc_to_system_mul * 2^(tsc_shift-32) / 100 by scale / 2^64:
852 * nsec/100 = ticks * scale / 2^64
853 * - tsc_timestamp * scale / 2^64
854 * + system_time / 100
856 * Equate with the Hyper-V formula so that ticks * scale / 2^64 cancels out:
857 * offset = system_time / 100 - tsc_timestamp * scale / 2^64
859 * These two equivalencies are implemented in this function.
861 static bool compute_tsc_page_parameters(struct pvclock_vcpu_time_info
*hv_clock
,
862 HV_REFERENCE_TSC_PAGE
*tsc_ref
)
866 if (!(hv_clock
->flags
& PVCLOCK_TSC_STABLE_BIT
))
870 * check if scale would overflow, if so we use the time ref counter
871 * tsc_to_system_mul * 2^(tsc_shift+32) / 100 >= 2^64
872 * tsc_to_system_mul / 100 >= 2^(32-tsc_shift)
873 * tsc_to_system_mul >= 100 * 2^(32-tsc_shift)
875 max_mul
= 100ull << (32 - hv_clock
->tsc_shift
);
876 if (hv_clock
->tsc_to_system_mul
>= max_mul
)
880 * Otherwise compute the scale and offset according to the formulas
884 mul_u64_u32_div(1ULL << (32 + hv_clock
->tsc_shift
),
885 hv_clock
->tsc_to_system_mul
,
888 tsc_ref
->tsc_offset
= hv_clock
->system_time
;
889 do_div(tsc_ref
->tsc_offset
, 100);
890 tsc_ref
->tsc_offset
-=
891 mul_u64_u64_shr(hv_clock
->tsc_timestamp
, tsc_ref
->tsc_scale
, 64);
895 void kvm_hv_setup_tsc_page(struct kvm
*kvm
,
896 struct pvclock_vcpu_time_info
*hv_clock
)
898 struct kvm_hv
*hv
= &kvm
->arch
.hyperv
;
902 BUILD_BUG_ON(sizeof(tsc_seq
) != sizeof(hv
->tsc_ref
.tsc_sequence
));
903 BUILD_BUG_ON(offsetof(HV_REFERENCE_TSC_PAGE
, tsc_sequence
) != 0);
905 if (!(hv
->hv_tsc_page
& HV_X64_MSR_TSC_REFERENCE_ENABLE
))
908 mutex_lock(&kvm
->arch
.hyperv
.hv_lock
);
909 if (!(hv
->hv_tsc_page
& HV_X64_MSR_TSC_REFERENCE_ENABLE
))
912 gfn
= hv
->hv_tsc_page
>> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT
;
914 * Because the TSC parameters only vary when there is a
915 * change in the master clock, do not bother with caching.
917 if (unlikely(kvm_read_guest(kvm
, gfn_to_gpa(gfn
),
918 &tsc_seq
, sizeof(tsc_seq
))))
922 * While we're computing and writing the parameters, force the
923 * guest to use the time reference count MSR.
925 hv
->tsc_ref
.tsc_sequence
= 0;
926 if (kvm_write_guest(kvm
, gfn_to_gpa(gfn
),
927 &hv
->tsc_ref
, sizeof(hv
->tsc_ref
.tsc_sequence
)))
930 if (!compute_tsc_page_parameters(hv_clock
, &hv
->tsc_ref
))
933 /* Ensure sequence is zero before writing the rest of the struct. */
935 if (kvm_write_guest(kvm
, gfn_to_gpa(gfn
), &hv
->tsc_ref
, sizeof(hv
->tsc_ref
)))
939 * Now switch to the TSC page mechanism by writing the sequence.
942 if (tsc_seq
== 0xFFFFFFFF || tsc_seq
== 0)
945 /* Write the struct entirely before the non-zero sequence. */
948 hv
->tsc_ref
.tsc_sequence
= tsc_seq
;
949 kvm_write_guest(kvm
, gfn_to_gpa(gfn
),
950 &hv
->tsc_ref
, sizeof(hv
->tsc_ref
.tsc_sequence
));
952 mutex_unlock(&kvm
->arch
.hyperv
.hv_lock
);
955 static int kvm_hv_set_msr_pw(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
,
958 struct kvm
*kvm
= vcpu
->kvm
;
959 struct kvm_hv
*hv
= &kvm
->arch
.hyperv
;
962 case HV_X64_MSR_GUEST_OS_ID
:
963 hv
->hv_guest_os_id
= data
;
964 /* setting guest os id to zero disables hypercall page */
965 if (!hv
->hv_guest_os_id
)
966 hv
->hv_hypercall
&= ~HV_X64_MSR_HYPERCALL_ENABLE
;
968 case HV_X64_MSR_HYPERCALL
: {
973 /* if guest os id is not set hypercall should remain disabled */
974 if (!hv
->hv_guest_os_id
)
976 if (!(data
& HV_X64_MSR_HYPERCALL_ENABLE
)) {
977 hv
->hv_hypercall
= data
;
980 gfn
= data
>> HV_X64_MSR_HYPERCALL_PAGE_ADDRESS_SHIFT
;
981 addr
= gfn_to_hva(kvm
, gfn
);
982 if (kvm_is_error_hva(addr
))
984 kvm_x86_ops
->patch_hypercall(vcpu
, instructions
);
985 ((unsigned char *)instructions
)[3] = 0xc3; /* ret */
986 if (__copy_to_user((void __user
*)addr
, instructions
, 4))
988 hv
->hv_hypercall
= data
;
989 mark_page_dirty(kvm
, gfn
);
992 case HV_X64_MSR_REFERENCE_TSC
:
993 hv
->hv_tsc_page
= data
;
994 if (hv
->hv_tsc_page
& HV_X64_MSR_TSC_REFERENCE_ENABLE
)
995 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE
, vcpu
);
997 case HV_X64_MSR_CRASH_P0
... HV_X64_MSR_CRASH_P4
:
998 return kvm_hv_msr_set_crash_data(vcpu
,
999 msr
- HV_X64_MSR_CRASH_P0
,
1001 case HV_X64_MSR_CRASH_CTL
:
1002 return kvm_hv_msr_set_crash_ctl(vcpu
, data
, host
);
1003 case HV_X64_MSR_RESET
:
1005 vcpu_debug(vcpu
, "hyper-v reset requested\n");
1006 kvm_make_request(KVM_REQ_HV_RESET
, vcpu
);
1009 case HV_X64_MSR_REENLIGHTENMENT_CONTROL
:
1010 hv
->hv_reenlightenment_control
= data
;
1012 case HV_X64_MSR_TSC_EMULATION_CONTROL
:
1013 hv
->hv_tsc_emulation_control
= data
;
1015 case HV_X64_MSR_TSC_EMULATION_STATUS
:
1016 hv
->hv_tsc_emulation_status
= data
;
1018 case HV_X64_MSR_TIME_REF_COUNT
:
1019 /* read-only, but still ignore it if host-initiated */
1024 vcpu_unimpl(vcpu
, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
1031 /* Calculate cpu time spent by current task in 100ns units */
1032 static u64
current_task_runtime_100ns(void)
1036 task_cputime_adjusted(current
, &utime
, &stime
);
1038 return div_u64(utime
+ stime
, 100);
1041 static int kvm_hv_set_msr(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
, bool host
)
1043 struct kvm_vcpu_hv
*hv
= &vcpu
->arch
.hyperv
;
1046 case HV_X64_MSR_VP_INDEX
:
1049 hv
->vp_index
= (u32
)data
;
1051 case HV_X64_MSR_VP_ASSIST_PAGE
: {
1055 if (!(data
& HV_X64_MSR_VP_ASSIST_PAGE_ENABLE
)) {
1056 hv
->hv_vapic
= data
;
1057 if (kvm_lapic_enable_pv_eoi(vcpu
, 0))
1061 gfn
= data
>> HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT
;
1062 addr
= kvm_vcpu_gfn_to_hva(vcpu
, gfn
);
1063 if (kvm_is_error_hva(addr
))
1065 if (__clear_user((void __user
*)addr
, PAGE_SIZE
))
1067 hv
->hv_vapic
= data
;
1068 kvm_vcpu_mark_page_dirty(vcpu
, gfn
);
1069 if (kvm_lapic_enable_pv_eoi(vcpu
,
1070 gfn_to_gpa(gfn
) | KVM_MSR_ENABLED
))
1074 case HV_X64_MSR_EOI
:
1075 return kvm_hv_vapic_msr_write(vcpu
, APIC_EOI
, data
);
1076 case HV_X64_MSR_ICR
:
1077 return kvm_hv_vapic_msr_write(vcpu
, APIC_ICR
, data
);
1078 case HV_X64_MSR_TPR
:
1079 return kvm_hv_vapic_msr_write(vcpu
, APIC_TASKPRI
, data
);
1080 case HV_X64_MSR_VP_RUNTIME
:
1083 hv
->runtime_offset
= data
- current_task_runtime_100ns();
1085 case HV_X64_MSR_SCONTROL
:
1086 case HV_X64_MSR_SVERSION
:
1087 case HV_X64_MSR_SIEFP
:
1088 case HV_X64_MSR_SIMP
:
1089 case HV_X64_MSR_EOM
:
1090 case HV_X64_MSR_SINT0
... HV_X64_MSR_SINT15
:
1091 return synic_set_msr(vcpu_to_synic(vcpu
), msr
, data
, host
);
1092 case HV_X64_MSR_STIMER0_CONFIG
:
1093 case HV_X64_MSR_STIMER1_CONFIG
:
1094 case HV_X64_MSR_STIMER2_CONFIG
:
1095 case HV_X64_MSR_STIMER3_CONFIG
: {
1096 int timer_index
= (msr
- HV_X64_MSR_STIMER0_CONFIG
)/2;
1098 return stimer_set_config(vcpu_to_stimer(vcpu
, timer_index
),
1101 case HV_X64_MSR_STIMER0_COUNT
:
1102 case HV_X64_MSR_STIMER1_COUNT
:
1103 case HV_X64_MSR_STIMER2_COUNT
:
1104 case HV_X64_MSR_STIMER3_COUNT
: {
1105 int timer_index
= (msr
- HV_X64_MSR_STIMER0_COUNT
)/2;
1107 return stimer_set_count(vcpu_to_stimer(vcpu
, timer_index
),
1110 case HV_X64_MSR_TSC_FREQUENCY
:
1111 case HV_X64_MSR_APIC_FREQUENCY
:
1112 /* read-only, but still ignore it if host-initiated */
1117 vcpu_unimpl(vcpu
, "Hyper-V uhandled wrmsr: 0x%x data 0x%llx\n",
1125 static int kvm_hv_get_msr_pw(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
)
1128 struct kvm
*kvm
= vcpu
->kvm
;
1129 struct kvm_hv
*hv
= &kvm
->arch
.hyperv
;
1132 case HV_X64_MSR_GUEST_OS_ID
:
1133 data
= hv
->hv_guest_os_id
;
1135 case HV_X64_MSR_HYPERCALL
:
1136 data
= hv
->hv_hypercall
;
1138 case HV_X64_MSR_TIME_REF_COUNT
:
1139 data
= get_time_ref_counter(kvm
);
1141 case HV_X64_MSR_REFERENCE_TSC
:
1142 data
= hv
->hv_tsc_page
;
1144 case HV_X64_MSR_CRASH_P0
... HV_X64_MSR_CRASH_P4
:
1145 return kvm_hv_msr_get_crash_data(vcpu
,
1146 msr
- HV_X64_MSR_CRASH_P0
,
1148 case HV_X64_MSR_CRASH_CTL
:
1149 return kvm_hv_msr_get_crash_ctl(vcpu
, pdata
);
1150 case HV_X64_MSR_RESET
:
1153 case HV_X64_MSR_REENLIGHTENMENT_CONTROL
:
1154 data
= hv
->hv_reenlightenment_control
;
1156 case HV_X64_MSR_TSC_EMULATION_CONTROL
:
1157 data
= hv
->hv_tsc_emulation_control
;
1159 case HV_X64_MSR_TSC_EMULATION_STATUS
:
1160 data
= hv
->hv_tsc_emulation_status
;
1163 vcpu_unimpl(vcpu
, "Hyper-V unhandled rdmsr: 0x%x\n", msr
);
1171 static int kvm_hv_get_msr(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
,
1175 struct kvm_vcpu_hv
*hv
= &vcpu
->arch
.hyperv
;
1178 case HV_X64_MSR_VP_INDEX
:
1179 data
= hv
->vp_index
;
1181 case HV_X64_MSR_EOI
:
1182 return kvm_hv_vapic_msr_read(vcpu
, APIC_EOI
, pdata
);
1183 case HV_X64_MSR_ICR
:
1184 return kvm_hv_vapic_msr_read(vcpu
, APIC_ICR
, pdata
);
1185 case HV_X64_MSR_TPR
:
1186 return kvm_hv_vapic_msr_read(vcpu
, APIC_TASKPRI
, pdata
);
1187 case HV_X64_MSR_VP_ASSIST_PAGE
:
1188 data
= hv
->hv_vapic
;
1190 case HV_X64_MSR_VP_RUNTIME
:
1191 data
= current_task_runtime_100ns() + hv
->runtime_offset
;
1193 case HV_X64_MSR_SCONTROL
:
1194 case HV_X64_MSR_SVERSION
:
1195 case HV_X64_MSR_SIEFP
:
1196 case HV_X64_MSR_SIMP
:
1197 case HV_X64_MSR_EOM
:
1198 case HV_X64_MSR_SINT0
... HV_X64_MSR_SINT15
:
1199 return synic_get_msr(vcpu_to_synic(vcpu
), msr
, pdata
, host
);
1200 case HV_X64_MSR_STIMER0_CONFIG
:
1201 case HV_X64_MSR_STIMER1_CONFIG
:
1202 case HV_X64_MSR_STIMER2_CONFIG
:
1203 case HV_X64_MSR_STIMER3_CONFIG
: {
1204 int timer_index
= (msr
- HV_X64_MSR_STIMER0_CONFIG
)/2;
1206 return stimer_get_config(vcpu_to_stimer(vcpu
, timer_index
),
1209 case HV_X64_MSR_STIMER0_COUNT
:
1210 case HV_X64_MSR_STIMER1_COUNT
:
1211 case HV_X64_MSR_STIMER2_COUNT
:
1212 case HV_X64_MSR_STIMER3_COUNT
: {
1213 int timer_index
= (msr
- HV_X64_MSR_STIMER0_COUNT
)/2;
1215 return stimer_get_count(vcpu_to_stimer(vcpu
, timer_index
),
1218 case HV_X64_MSR_TSC_FREQUENCY
:
1219 data
= (u64
)vcpu
->arch
.virtual_tsc_khz
* 1000;
1221 case HV_X64_MSR_APIC_FREQUENCY
:
1222 data
= APIC_BUS_FREQUENCY
;
1225 vcpu_unimpl(vcpu
, "Hyper-V unhandled rdmsr: 0x%x\n", msr
);
1232 int kvm_hv_set_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64 data
, bool host
)
1234 if (kvm_hv_msr_partition_wide(msr
)) {
1237 mutex_lock(&vcpu
->kvm
->arch
.hyperv
.hv_lock
);
1238 r
= kvm_hv_set_msr_pw(vcpu
, msr
, data
, host
);
1239 mutex_unlock(&vcpu
->kvm
->arch
.hyperv
.hv_lock
);
1242 return kvm_hv_set_msr(vcpu
, msr
, data
, host
);
1245 int kvm_hv_get_msr_common(struct kvm_vcpu
*vcpu
, u32 msr
, u64
*pdata
, bool host
)
1247 if (kvm_hv_msr_partition_wide(msr
)) {
1250 mutex_lock(&vcpu
->kvm
->arch
.hyperv
.hv_lock
);
1251 r
= kvm_hv_get_msr_pw(vcpu
, msr
, pdata
);
1252 mutex_unlock(&vcpu
->kvm
->arch
.hyperv
.hv_lock
);
1255 return kvm_hv_get_msr(vcpu
, msr
, pdata
, host
);
1258 static __always_inline
int get_sparse_bank_no(u64 valid_bank_mask
, int bank_no
)
1262 if (!(valid_bank_mask
& BIT_ULL(bank_no
)))
1265 for (j
= 0; j
< bank_no
; j
++)
1266 if (valid_bank_mask
& BIT_ULL(j
))
1272 static u64
kvm_hv_flush_tlb(struct kvm_vcpu
*current_vcpu
, u64 ingpa
,
1273 u16 rep_cnt
, bool ex
)
1275 struct kvm
*kvm
= current_vcpu
->kvm
;
1276 struct kvm_vcpu_hv
*hv_current
= ¤t_vcpu
->arch
.hyperv
;
1277 struct hv_tlb_flush_ex flush_ex
;
1278 struct hv_tlb_flush flush
;
1279 struct kvm_vcpu
*vcpu
;
1280 unsigned long vcpu_bitmap
[BITS_TO_LONGS(KVM_MAX_VCPUS
)] = {0};
1281 unsigned long valid_bank_mask
= 0;
1282 u64 sparse_banks
[64];
1283 int sparse_banks_len
, i
;
1287 if (unlikely(kvm_read_guest(kvm
, ingpa
, &flush
, sizeof(flush
))))
1288 return HV_STATUS_INVALID_HYPERCALL_INPUT
;
1290 trace_kvm_hv_flush_tlb(flush
.processor_mask
,
1291 flush
.address_space
, flush
.flags
);
1293 sparse_banks
[0] = flush
.processor_mask
;
1296 * Work around possible WS2012 bug: it sends hypercalls
1297 * with processor_mask = 0x0 and HV_FLUSH_ALL_PROCESSORS clear,
1298 * while also expecting us to flush something and crashing if
1299 * we don't. Let's treat processor_mask == 0 same as
1300 * HV_FLUSH_ALL_PROCESSORS.
1302 all_cpus
= (flush
.flags
& HV_FLUSH_ALL_PROCESSORS
) ||
1303 flush
.processor_mask
== 0;
1305 if (unlikely(kvm_read_guest(kvm
, ingpa
, &flush_ex
,
1307 return HV_STATUS_INVALID_HYPERCALL_INPUT
;
1309 trace_kvm_hv_flush_tlb_ex(flush_ex
.hv_vp_set
.valid_bank_mask
,
1310 flush_ex
.hv_vp_set
.format
,
1311 flush_ex
.address_space
,
1314 valid_bank_mask
= flush_ex
.hv_vp_set
.valid_bank_mask
;
1315 all_cpus
= flush_ex
.hv_vp_set
.format
!=
1316 HV_GENERIC_SET_SPARSE_4K
;
1318 sparse_banks_len
= bitmap_weight(&valid_bank_mask
, 64) *
1319 sizeof(sparse_banks
[0]);
1321 if (!sparse_banks_len
&& !all_cpus
)
1326 ingpa
+ offsetof(struct hv_tlb_flush_ex
,
1327 hv_vp_set
.bank_contents
),
1330 return HV_STATUS_INVALID_HYPERCALL_INPUT
;
1333 cpumask_clear(&hv_current
->tlb_lush
);
1335 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
1336 struct kvm_vcpu_hv
*hv
= &vcpu
->arch
.hyperv
;
1337 int bank
= hv
->vp_index
/ 64, sbank
= 0;
1340 /* Banks >64 can't be represented */
1344 /* Non-ex hypercalls can only address first 64 vCPUs */
1350 * Check is the bank of this vCPU is in sparse
1351 * set and get the sparse bank number.
1353 sbank
= get_sparse_bank_no(valid_bank_mask
,
1360 if (!(sparse_banks
[sbank
] & BIT_ULL(hv
->vp_index
% 64)))
1365 * vcpu->arch.cr3 may not be up-to-date for running vCPUs so we
1366 * can't analyze it here, flush TLB regardless of the specified
1369 __set_bit(i
, vcpu_bitmap
);
1372 kvm_make_vcpus_request_mask(kvm
,
1373 KVM_REQ_TLB_FLUSH
| KVM_REQUEST_NO_WAKEUP
,
1374 vcpu_bitmap
, &hv_current
->tlb_lush
);
1377 /* We always do full TLB flush, set rep_done = rep_cnt. */
1378 return (u64
)HV_STATUS_SUCCESS
|
1379 ((u64
)rep_cnt
<< HV_HYPERCALL_REP_COMP_OFFSET
);
1382 bool kvm_hv_hypercall_enabled(struct kvm
*kvm
)
1384 return READ_ONCE(kvm
->arch
.hyperv
.hv_hypercall
) & HV_X64_MSR_HYPERCALL_ENABLE
;
1387 static void kvm_hv_hypercall_set_result(struct kvm_vcpu
*vcpu
, u64 result
)
1391 longmode
= is_64_bit_mode(vcpu
);
1393 kvm_register_write(vcpu
, VCPU_REGS_RAX
, result
);
1395 kvm_register_write(vcpu
, VCPU_REGS_RDX
, result
>> 32);
1396 kvm_register_write(vcpu
, VCPU_REGS_RAX
, result
& 0xffffffff);
1400 static int kvm_hv_hypercall_complete(struct kvm_vcpu
*vcpu
, u64 result
)
1402 kvm_hv_hypercall_set_result(vcpu
, result
);
1403 ++vcpu
->stat
.hypercalls
;
1404 return kvm_skip_emulated_instruction(vcpu
);
1407 static int kvm_hv_hypercall_complete_userspace(struct kvm_vcpu
*vcpu
)
1409 return kvm_hv_hypercall_complete(vcpu
, vcpu
->run
->hyperv
.u
.hcall
.result
);
1412 static u16
kvm_hvcall_signal_event(struct kvm_vcpu
*vcpu
, bool fast
, u64 param
)
1414 struct eventfd_ctx
*eventfd
;
1416 if (unlikely(!fast
)) {
1420 if ((gpa
& (__alignof__(param
) - 1)) ||
1421 offset_in_page(gpa
) + sizeof(param
) > PAGE_SIZE
)
1422 return HV_STATUS_INVALID_ALIGNMENT
;
1424 ret
= kvm_vcpu_read_guest(vcpu
, gpa
, ¶m
, sizeof(param
));
1426 return HV_STATUS_INVALID_ALIGNMENT
;
1430 * Per spec, bits 32-47 contain the extra "flag number". However, we
1431 * have no use for it, and in all known usecases it is zero, so just
1432 * report lookup failure if it isn't.
1434 if (param
& 0xffff00000000ULL
)
1435 return HV_STATUS_INVALID_PORT_ID
;
1436 /* remaining bits are reserved-zero */
1437 if (param
& ~KVM_HYPERV_CONN_ID_MASK
)
1438 return HV_STATUS_INVALID_HYPERCALL_INPUT
;
1440 /* the eventfd is protected by vcpu->kvm->srcu, but conn_to_evt isn't */
1442 eventfd
= idr_find(&vcpu
->kvm
->arch
.hyperv
.conn_to_evt
, param
);
1445 return HV_STATUS_INVALID_PORT_ID
;
1447 eventfd_signal(eventfd
, 1);
1448 return HV_STATUS_SUCCESS
;
1451 int kvm_hv_hypercall(struct kvm_vcpu
*vcpu
)
1453 u64 param
, ingpa
, outgpa
, ret
= HV_STATUS_SUCCESS
;
1454 uint16_t code
, rep_idx
, rep_cnt
;
1455 bool fast
, longmode
, rep
;
1458 * hypercall generates UD from non zero cpl and real mode
1461 if (kvm_x86_ops
->get_cpl(vcpu
) != 0 || !is_protmode(vcpu
)) {
1462 kvm_queue_exception(vcpu
, UD_VECTOR
);
1466 longmode
= is_64_bit_mode(vcpu
);
1469 param
= ((u64
)kvm_register_read(vcpu
, VCPU_REGS_RDX
) << 32) |
1470 (kvm_register_read(vcpu
, VCPU_REGS_RAX
) & 0xffffffff);
1471 ingpa
= ((u64
)kvm_register_read(vcpu
, VCPU_REGS_RBX
) << 32) |
1472 (kvm_register_read(vcpu
, VCPU_REGS_RCX
) & 0xffffffff);
1473 outgpa
= ((u64
)kvm_register_read(vcpu
, VCPU_REGS_RDI
) << 32) |
1474 (kvm_register_read(vcpu
, VCPU_REGS_RSI
) & 0xffffffff);
1476 #ifdef CONFIG_X86_64
1478 param
= kvm_register_read(vcpu
, VCPU_REGS_RCX
);
1479 ingpa
= kvm_register_read(vcpu
, VCPU_REGS_RDX
);
1480 outgpa
= kvm_register_read(vcpu
, VCPU_REGS_R8
);
1484 code
= param
& 0xffff;
1485 fast
= !!(param
& HV_HYPERCALL_FAST_BIT
);
1486 rep_cnt
= (param
>> HV_HYPERCALL_REP_COMP_OFFSET
) & 0xfff;
1487 rep_idx
= (param
>> HV_HYPERCALL_REP_START_OFFSET
) & 0xfff;
1488 rep
= !!(rep_cnt
|| rep_idx
);
1490 trace_kvm_hv_hypercall(code
, fast
, rep_cnt
, rep_idx
, ingpa
, outgpa
);
1493 case HVCALL_NOTIFY_LONG_SPIN_WAIT
:
1494 if (unlikely(rep
)) {
1495 ret
= HV_STATUS_INVALID_HYPERCALL_INPUT
;
1498 kvm_vcpu_on_spin(vcpu
, true);
1500 case HVCALL_SIGNAL_EVENT
:
1501 if (unlikely(rep
)) {
1502 ret
= HV_STATUS_INVALID_HYPERCALL_INPUT
;
1505 ret
= kvm_hvcall_signal_event(vcpu
, fast
, ingpa
);
1506 if (ret
!= HV_STATUS_INVALID_PORT_ID
)
1508 /* maybe userspace knows this conn_id: fall through */
1509 case HVCALL_POST_MESSAGE
:
1510 /* don't bother userspace if it has no way to handle it */
1511 if (unlikely(rep
|| !vcpu_to_synic(vcpu
)->active
)) {
1512 ret
= HV_STATUS_INVALID_HYPERCALL_INPUT
;
1515 vcpu
->run
->exit_reason
= KVM_EXIT_HYPERV
;
1516 vcpu
->run
->hyperv
.type
= KVM_EXIT_HYPERV_HCALL
;
1517 vcpu
->run
->hyperv
.u
.hcall
.input
= param
;
1518 vcpu
->run
->hyperv
.u
.hcall
.params
[0] = ingpa
;
1519 vcpu
->run
->hyperv
.u
.hcall
.params
[1] = outgpa
;
1520 vcpu
->arch
.complete_userspace_io
=
1521 kvm_hv_hypercall_complete_userspace
;
1523 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST
:
1524 if (unlikely(fast
|| !rep_cnt
|| rep_idx
)) {
1525 ret
= HV_STATUS_INVALID_HYPERCALL_INPUT
;
1528 ret
= kvm_hv_flush_tlb(vcpu
, ingpa
, rep_cnt
, false);
1530 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE
:
1531 if (unlikely(fast
|| rep
)) {
1532 ret
= HV_STATUS_INVALID_HYPERCALL_INPUT
;
1535 ret
= kvm_hv_flush_tlb(vcpu
, ingpa
, rep_cnt
, false);
1537 case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX
:
1538 if (unlikely(fast
|| !rep_cnt
|| rep_idx
)) {
1539 ret
= HV_STATUS_INVALID_HYPERCALL_INPUT
;
1542 ret
= kvm_hv_flush_tlb(vcpu
, ingpa
, rep_cnt
, true);
1544 case HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX
:
1545 if (unlikely(fast
|| rep
)) {
1546 ret
= HV_STATUS_INVALID_HYPERCALL_INPUT
;
1549 ret
= kvm_hv_flush_tlb(vcpu
, ingpa
, rep_cnt
, true);
1552 ret
= HV_STATUS_INVALID_HYPERCALL_CODE
;
1556 return kvm_hv_hypercall_complete(vcpu
, ret
);
1559 void kvm_hv_init_vm(struct kvm
*kvm
)
1561 mutex_init(&kvm
->arch
.hyperv
.hv_lock
);
1562 idr_init(&kvm
->arch
.hyperv
.conn_to_evt
);
1565 void kvm_hv_destroy_vm(struct kvm
*kvm
)
1567 struct eventfd_ctx
*eventfd
;
1570 idr_for_each_entry(&kvm
->arch
.hyperv
.conn_to_evt
, eventfd
, i
)
1571 eventfd_ctx_put(eventfd
);
1572 idr_destroy(&kvm
->arch
.hyperv
.conn_to_evt
);
1575 static int kvm_hv_eventfd_assign(struct kvm
*kvm
, u32 conn_id
, int fd
)
1577 struct kvm_hv
*hv
= &kvm
->arch
.hyperv
;
1578 struct eventfd_ctx
*eventfd
;
1581 eventfd
= eventfd_ctx_fdget(fd
);
1582 if (IS_ERR(eventfd
))
1583 return PTR_ERR(eventfd
);
1585 mutex_lock(&hv
->hv_lock
);
1586 ret
= idr_alloc(&hv
->conn_to_evt
, eventfd
, conn_id
, conn_id
+ 1,
1588 mutex_unlock(&hv
->hv_lock
);
1595 eventfd_ctx_put(eventfd
);
1599 static int kvm_hv_eventfd_deassign(struct kvm
*kvm
, u32 conn_id
)
1601 struct kvm_hv
*hv
= &kvm
->arch
.hyperv
;
1602 struct eventfd_ctx
*eventfd
;
1604 mutex_lock(&hv
->hv_lock
);
1605 eventfd
= idr_remove(&hv
->conn_to_evt
, conn_id
);
1606 mutex_unlock(&hv
->hv_lock
);
1611 synchronize_srcu(&kvm
->srcu
);
1612 eventfd_ctx_put(eventfd
);
1616 int kvm_vm_ioctl_hv_eventfd(struct kvm
*kvm
, struct kvm_hyperv_eventfd
*args
)
1618 if ((args
->flags
& ~KVM_HYPERV_EVENTFD_DEASSIGN
) ||
1619 (args
->conn_id
& ~KVM_HYPERV_CONN_ID_MASK
))
1622 if (args
->flags
== KVM_HYPERV_EVENTFD_DEASSIGN
)
1623 return kvm_hv_eventfd_deassign(kvm
, args
->conn_id
);
1624 return kvm_hv_eventfd_assign(kvm
, args
->conn_id
, args
->fd
);