1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright © 2019 Oracle and/or its affiliates. All rights reserved.
4 * Copyright © 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
14 #include <linux/eventfd.h>
15 #include <linux/kvm_host.h>
16 #include <linux/sched/stat.h>
18 #include <trace/events/kvm.h>
19 #include <xen/interface/xen.h>
20 #include <xen/interface/vcpu.h>
21 #include <xen/interface/version.h>
22 #include <xen/interface/event_channel.h>
23 #include <xen/interface/sched.h>
27 static int kvm_xen_set_evtchn(struct kvm_xen_evtchn *xe, struct kvm *kvm);
28 static int kvm_xen_setattr_evtchn(struct kvm *kvm, struct kvm_xen_hvm_attr *data);
29 static bool kvm_xen_hcall_evtchn_send(struct kvm_vcpu *vcpu, u64 param, u64 *r);
31 DEFINE_STATIC_KEY_DEFERRED_FALSE(kvm_xen_enabled, HZ);
33 static int kvm_xen_shared_info_init(struct kvm *kvm, gfn_t gfn)
35 struct gfn_to_pfn_cache *gpc = &kvm->arch.xen.shinfo_cache;
36 struct pvclock_wall_clock *wc;
37 gpa_t gpa = gfn_to_gpa(gfn);
42 int idx = srcu_read_lock(&kvm->srcu);
44 if (gfn == GPA_INVALID) {
45 kvm_gfn_to_pfn_cache_destroy(kvm, gpc);
50 ret = kvm_gfn_to_pfn_cache_init(kvm, gpc, NULL, KVM_HOST_USES_PFN,
56 * This code mirrors kvm_write_wall_clock() except that it writes
57 * directly through the pfn cache and doesn't mark the page dirty.
59 wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm);
61 /* It could be invalid again already, so we need to check */
62 read_lock_irq(&gpc->lock);
67 read_unlock_irq(&gpc->lock);
70 /* Paranoia checks on the 32-bit struct layout */
71 BUILD_BUG_ON(offsetof(struct compat_shared_info, wc) != 0x900);
72 BUILD_BUG_ON(offsetof(struct compat_shared_info, arch.wc_sec_hi) != 0x924);
73 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
76 /* Paranoia checks on the 64-bit struct layout */
77 BUILD_BUG_ON(offsetof(struct shared_info, wc) != 0xc00);
78 BUILD_BUG_ON(offsetof(struct shared_info, wc_sec_hi) != 0xc0c);
80 if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) {
81 struct shared_info *shinfo = gpc->khva;
83 wc_sec_hi = &shinfo->wc_sec_hi;
88 struct compat_shared_info *shinfo = gpc->khva;
90 wc_sec_hi = &shinfo->arch.wc_sec_hi;
94 /* Increment and ensure an odd value */
95 wc_version = wc->version = (wc->version + 1) | 1;
98 wc->nsec = do_div(wall_nsec, 1000000000);
99 wc->sec = (u32)wall_nsec;
100 *wc_sec_hi = wall_nsec >> 32;
103 wc->version = wc_version + 1;
104 read_unlock_irq(&gpc->lock);
106 kvm_make_all_cpus_request(kvm, KVM_REQ_MASTERCLOCK_UPDATE);
109 srcu_read_unlock(&kvm->srcu, idx);
113 void kvm_xen_inject_timer_irqs(struct kvm_vcpu *vcpu)
115 if (atomic_read(&vcpu->arch.xen.timer_pending) > 0) {
116 struct kvm_xen_evtchn e;
118 e.vcpu_id = vcpu->vcpu_id;
119 e.vcpu_idx = vcpu->vcpu_idx;
120 e.port = vcpu->arch.xen.timer_virq;
121 e.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL;
123 kvm_xen_set_evtchn(&e, vcpu->kvm);
125 vcpu->arch.xen.timer_expires = 0;
126 atomic_set(&vcpu->arch.xen.timer_pending, 0);
130 static enum hrtimer_restart xen_timer_callback(struct hrtimer *timer)
132 struct kvm_vcpu *vcpu = container_of(timer, struct kvm_vcpu,
134 if (atomic_read(&vcpu->arch.xen.timer_pending))
135 return HRTIMER_NORESTART;
137 atomic_inc(&vcpu->arch.xen.timer_pending);
138 kvm_make_request(KVM_REQ_UNBLOCK, vcpu);
141 return HRTIMER_NORESTART;
144 static void kvm_xen_start_timer(struct kvm_vcpu *vcpu, u64 guest_abs, s64 delta_ns)
146 atomic_set(&vcpu->arch.xen.timer_pending, 0);
147 vcpu->arch.xen.timer_expires = guest_abs;
150 xen_timer_callback(&vcpu->arch.xen.timer);
152 ktime_t ktime_now = ktime_get();
153 hrtimer_start(&vcpu->arch.xen.timer,
154 ktime_add_ns(ktime_now, delta_ns),
155 HRTIMER_MODE_ABS_HARD);
159 static void kvm_xen_stop_timer(struct kvm_vcpu *vcpu)
161 hrtimer_cancel(&vcpu->arch.xen.timer);
162 vcpu->arch.xen.timer_expires = 0;
163 atomic_set(&vcpu->arch.xen.timer_pending, 0);
166 static void kvm_xen_init_timer(struct kvm_vcpu *vcpu)
168 hrtimer_init(&vcpu->arch.xen.timer, CLOCK_MONOTONIC,
169 HRTIMER_MODE_ABS_HARD);
170 vcpu->arch.xen.timer.function = xen_timer_callback;
173 static void kvm_xen_update_runstate(struct kvm_vcpu *v, int state)
175 struct kvm_vcpu_xen *vx = &v->arch.xen;
176 u64 now = get_kvmclock_ns(v->kvm);
177 u64 delta_ns = now - vx->runstate_entry_time;
178 u64 run_delay = current->sched_info.run_delay;
180 if (unlikely(!vx->runstate_entry_time))
181 vx->current_runstate = RUNSTATE_offline;
184 * Time waiting for the scheduler isn't "stolen" if the
185 * vCPU wasn't running anyway.
187 if (vx->current_runstate == RUNSTATE_running) {
188 u64 steal_ns = run_delay - vx->last_steal;
190 delta_ns -= steal_ns;
192 vx->runstate_times[RUNSTATE_runnable] += steal_ns;
194 vx->last_steal = run_delay;
196 vx->runstate_times[vx->current_runstate] += delta_ns;
197 vx->current_runstate = state;
198 vx->runstate_entry_time = now;
201 void kvm_xen_update_runstate_guest(struct kvm_vcpu *v, int state)
203 struct kvm_vcpu_xen *vx = &v->arch.xen;
204 struct gfn_to_pfn_cache *gpc = &vx->runstate_cache;
205 uint64_t *user_times;
210 kvm_xen_update_runstate(v, state);
212 if (!vx->runstate_cache.active)
215 if (IS_ENABLED(CONFIG_64BIT) && v->kvm->arch.xen.long_mode)
216 user_len = sizeof(struct vcpu_runstate_info);
218 user_len = sizeof(struct compat_vcpu_runstate_info);
220 read_lock_irqsave(&gpc->lock, flags);
221 while (!kvm_gfn_to_pfn_cache_check(v->kvm, gpc, gpc->gpa,
223 read_unlock_irqrestore(&gpc->lock, flags);
225 /* When invoked from kvm_sched_out() we cannot sleep */
226 if (state == RUNSTATE_runnable)
229 if (kvm_gfn_to_pfn_cache_refresh(v->kvm, gpc, gpc->gpa, user_len))
232 read_lock_irqsave(&gpc->lock, flags);
236 * The only difference between 32-bit and 64-bit versions of the
237 * runstate struct us the alignment of uint64_t in 32-bit, which
238 * means that the 64-bit version has an additional 4 bytes of
239 * padding after the first field 'state'.
241 * So we use 'int __user *user_state' to point to the state field,
242 * and 'uint64_t __user *user_times' for runstate_entry_time. So
243 * the actual array of time[] in each state starts at user_times[1].
245 BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, state) != 0);
246 BUILD_BUG_ON(offsetof(struct compat_vcpu_runstate_info, state) != 0);
247 BUILD_BUG_ON(sizeof(struct compat_vcpu_runstate_info) != 0x2c);
249 BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, state_entry_time) !=
250 offsetof(struct compat_vcpu_runstate_info, state_entry_time) + 4);
251 BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, time) !=
252 offsetof(struct compat_vcpu_runstate_info, time) + 4);
255 user_state = gpc->khva;
257 if (IS_ENABLED(CONFIG_64BIT) && v->kvm->arch.xen.long_mode)
258 user_times = gpc->khva + offsetof(struct vcpu_runstate_info,
261 user_times = gpc->khva + offsetof(struct compat_vcpu_runstate_info,
265 * First write the updated state_entry_time at the appropriate
266 * location determined by 'offset'.
268 BUILD_BUG_ON(sizeof_field(struct vcpu_runstate_info, state_entry_time) !=
269 sizeof(user_times[0]));
270 BUILD_BUG_ON(sizeof_field(struct compat_vcpu_runstate_info, state_entry_time) !=
271 sizeof(user_times[0]));
273 user_times[0] = vx->runstate_entry_time | XEN_RUNSTATE_UPDATE;
277 * Next, write the new runstate. This is in the *same* place
278 * for 32-bit and 64-bit guests, asserted here for paranoia.
280 BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, state) !=
281 offsetof(struct compat_vcpu_runstate_info, state));
282 BUILD_BUG_ON(sizeof_field(struct vcpu_runstate_info, state) !=
283 sizeof(vx->current_runstate));
284 BUILD_BUG_ON(sizeof_field(struct compat_vcpu_runstate_info, state) !=
285 sizeof(vx->current_runstate));
287 *user_state = vx->current_runstate;
290 * Write the actual runstate times immediately after the
291 * runstate_entry_time.
293 BUILD_BUG_ON(offsetof(struct vcpu_runstate_info, state_entry_time) !=
294 offsetof(struct vcpu_runstate_info, time) - sizeof(u64));
295 BUILD_BUG_ON(offsetof(struct compat_vcpu_runstate_info, state_entry_time) !=
296 offsetof(struct compat_vcpu_runstate_info, time) - sizeof(u64));
297 BUILD_BUG_ON(sizeof_field(struct vcpu_runstate_info, time) !=
298 sizeof_field(struct compat_vcpu_runstate_info, time));
299 BUILD_BUG_ON(sizeof_field(struct vcpu_runstate_info, time) !=
300 sizeof(vx->runstate_times));
302 memcpy(user_times + 1, vx->runstate_times, sizeof(vx->runstate_times));
306 * Finally, clear the XEN_RUNSTATE_UPDATE bit in the guest's
307 * runstate_entry_time field.
309 user_times[0] &= ~XEN_RUNSTATE_UPDATE;
312 read_unlock_irqrestore(&gpc->lock, flags);
314 mark_page_dirty_in_slot(v->kvm, gpc->memslot, gpc->gpa >> PAGE_SHIFT);
317 static void kvm_xen_inject_vcpu_vector(struct kvm_vcpu *v)
319 struct kvm_lapic_irq irq = { };
322 irq.dest_id = v->vcpu_id;
323 irq.vector = v->arch.xen.upcall_vector;
324 irq.dest_mode = APIC_DEST_PHYSICAL;
325 irq.shorthand = APIC_DEST_NOSHORT;
326 irq.delivery_mode = APIC_DM_FIXED;
329 /* The fast version will always work for physical unicast */
330 WARN_ON_ONCE(!kvm_irq_delivery_to_apic_fast(v->kvm, NULL, &irq, &r, NULL));
334 * On event channel delivery, the vcpu_info may not have been accessible.
335 * In that case, there are bits in vcpu->arch.xen.evtchn_pending_sel which
336 * need to be marked into the vcpu_info (and evtchn_upcall_pending set).
337 * Do so now that we can sleep in the context of the vCPU to bring the
338 * page in, and refresh the pfn cache for it.
340 void kvm_xen_inject_pending_events(struct kvm_vcpu *v)
342 unsigned long evtchn_pending_sel = READ_ONCE(v->arch.xen.evtchn_pending_sel);
343 struct gfn_to_pfn_cache *gpc = &v->arch.xen.vcpu_info_cache;
346 if (!evtchn_pending_sel)
350 * Yes, this is an open-coded loop. But that's just what put_user()
351 * does anyway. Page it in and retry the instruction. We're just a
352 * little more honest about it.
354 read_lock_irqsave(&gpc->lock, flags);
355 while (!kvm_gfn_to_pfn_cache_check(v->kvm, gpc, gpc->gpa,
356 sizeof(struct vcpu_info))) {
357 read_unlock_irqrestore(&gpc->lock, flags);
359 if (kvm_gfn_to_pfn_cache_refresh(v->kvm, gpc, gpc->gpa,
360 sizeof(struct vcpu_info)))
363 read_lock_irqsave(&gpc->lock, flags);
366 /* Now gpc->khva is a valid kernel address for the vcpu_info */
367 if (IS_ENABLED(CONFIG_64BIT) && v->kvm->arch.xen.long_mode) {
368 struct vcpu_info *vi = gpc->khva;
370 asm volatile(LOCK_PREFIX "orq %0, %1\n"
372 LOCK_PREFIX "andq %0, %2\n"
373 : "=r" (evtchn_pending_sel),
374 "+m" (vi->evtchn_pending_sel),
375 "+m" (v->arch.xen.evtchn_pending_sel)
376 : "0" (evtchn_pending_sel));
377 WRITE_ONCE(vi->evtchn_upcall_pending, 1);
379 u32 evtchn_pending_sel32 = evtchn_pending_sel;
380 struct compat_vcpu_info *vi = gpc->khva;
382 asm volatile(LOCK_PREFIX "orl %0, %1\n"
384 LOCK_PREFIX "andl %0, %2\n"
385 : "=r" (evtchn_pending_sel32),
386 "+m" (vi->evtchn_pending_sel),
387 "+m" (v->arch.xen.evtchn_pending_sel)
388 : "0" (evtchn_pending_sel32));
389 WRITE_ONCE(vi->evtchn_upcall_pending, 1);
391 read_unlock_irqrestore(&gpc->lock, flags);
393 /* For the per-vCPU lapic vector, deliver it as MSI. */
394 if (v->arch.xen.upcall_vector)
395 kvm_xen_inject_vcpu_vector(v);
397 mark_page_dirty_in_slot(v->kvm, gpc->memslot, gpc->gpa >> PAGE_SHIFT);
400 int __kvm_xen_has_interrupt(struct kvm_vcpu *v)
402 struct gfn_to_pfn_cache *gpc = &v->arch.xen.vcpu_info_cache;
407 * If the global upcall vector (HVMIRQ_callback_vector) is set and
408 * the vCPU's evtchn_upcall_pending flag is set, the IRQ is pending.
411 /* No need for compat handling here */
412 BUILD_BUG_ON(offsetof(struct vcpu_info, evtchn_upcall_pending) !=
413 offsetof(struct compat_vcpu_info, evtchn_upcall_pending));
414 BUILD_BUG_ON(sizeof(rc) !=
415 sizeof_field(struct vcpu_info, evtchn_upcall_pending));
416 BUILD_BUG_ON(sizeof(rc) !=
417 sizeof_field(struct compat_vcpu_info, evtchn_upcall_pending));
419 read_lock_irqsave(&gpc->lock, flags);
420 while (!kvm_gfn_to_pfn_cache_check(v->kvm, gpc, gpc->gpa,
421 sizeof(struct vcpu_info))) {
422 read_unlock_irqrestore(&gpc->lock, flags);
425 * This function gets called from kvm_vcpu_block() after setting the
426 * task to TASK_INTERRUPTIBLE, to see if it needs to wake immediately
427 * from a HLT. So we really mustn't sleep. If the page ended up absent
428 * at that point, just return 1 in order to trigger an immediate wake,
429 * and we'll end up getting called again from a context where we *can*
430 * fault in the page and wait for it.
432 if (in_atomic() || !task_is_running(current))
435 if (kvm_gfn_to_pfn_cache_refresh(v->kvm, gpc, gpc->gpa,
436 sizeof(struct vcpu_info))) {
438 * If this failed, userspace has screwed up the
439 * vcpu_info mapping. No interrupts for you.
443 read_lock_irqsave(&gpc->lock, flags);
446 rc = ((struct vcpu_info *)gpc->khva)->evtchn_upcall_pending;
447 read_unlock_irqrestore(&gpc->lock, flags);
451 int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
456 switch (data->type) {
457 case KVM_XEN_ATTR_TYPE_LONG_MODE:
458 if (!IS_ENABLED(CONFIG_64BIT) && data->u.long_mode) {
461 mutex_lock(&kvm->lock);
462 kvm->arch.xen.long_mode = !!data->u.long_mode;
463 mutex_unlock(&kvm->lock);
468 case KVM_XEN_ATTR_TYPE_SHARED_INFO:
469 mutex_lock(&kvm->lock);
470 r = kvm_xen_shared_info_init(kvm, data->u.shared_info.gfn);
471 mutex_unlock(&kvm->lock);
474 case KVM_XEN_ATTR_TYPE_UPCALL_VECTOR:
475 if (data->u.vector && data->u.vector < 0x10)
478 mutex_lock(&kvm->lock);
479 kvm->arch.xen.upcall_vector = data->u.vector;
480 mutex_unlock(&kvm->lock);
485 case KVM_XEN_ATTR_TYPE_EVTCHN:
486 r = kvm_xen_setattr_evtchn(kvm, data);
489 case KVM_XEN_ATTR_TYPE_XEN_VERSION:
490 mutex_lock(&kvm->lock);
491 kvm->arch.xen.xen_version = data->u.xen_version;
492 mutex_unlock(&kvm->lock);
503 int kvm_xen_hvm_get_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
507 mutex_lock(&kvm->lock);
509 switch (data->type) {
510 case KVM_XEN_ATTR_TYPE_LONG_MODE:
511 data->u.long_mode = kvm->arch.xen.long_mode;
515 case KVM_XEN_ATTR_TYPE_SHARED_INFO:
516 if (kvm->arch.xen.shinfo_cache.active)
517 data->u.shared_info.gfn = gpa_to_gfn(kvm->arch.xen.shinfo_cache.gpa);
519 data->u.shared_info.gfn = GPA_INVALID;
523 case KVM_XEN_ATTR_TYPE_UPCALL_VECTOR:
524 data->u.vector = kvm->arch.xen.upcall_vector;
528 case KVM_XEN_ATTR_TYPE_XEN_VERSION:
529 data->u.xen_version = kvm->arch.xen.xen_version;
537 mutex_unlock(&kvm->lock);
541 int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data)
543 int idx, r = -ENOENT;
545 mutex_lock(&vcpu->kvm->lock);
546 idx = srcu_read_lock(&vcpu->kvm->srcu);
548 switch (data->type) {
549 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO:
550 /* No compat necessary here. */
551 BUILD_BUG_ON(sizeof(struct vcpu_info) !=
552 sizeof(struct compat_vcpu_info));
553 BUILD_BUG_ON(offsetof(struct vcpu_info, time) !=
554 offsetof(struct compat_vcpu_info, time));
556 if (data->u.gpa == GPA_INVALID) {
557 kvm_gfn_to_pfn_cache_destroy(vcpu->kvm, &vcpu->arch.xen.vcpu_info_cache);
562 r = kvm_gfn_to_pfn_cache_init(vcpu->kvm,
563 &vcpu->arch.xen.vcpu_info_cache,
564 NULL, KVM_HOST_USES_PFN, data->u.gpa,
565 sizeof(struct vcpu_info));
567 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
571 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO:
572 if (data->u.gpa == GPA_INVALID) {
573 kvm_gfn_to_pfn_cache_destroy(vcpu->kvm,
574 &vcpu->arch.xen.vcpu_time_info_cache);
579 r = kvm_gfn_to_pfn_cache_init(vcpu->kvm,
580 &vcpu->arch.xen.vcpu_time_info_cache,
581 NULL, KVM_HOST_USES_PFN, data->u.gpa,
582 sizeof(struct pvclock_vcpu_time_info));
584 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
587 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR:
588 if (!sched_info_on()) {
592 if (data->u.gpa == GPA_INVALID) {
593 kvm_gfn_to_pfn_cache_destroy(vcpu->kvm,
594 &vcpu->arch.xen.runstate_cache);
599 r = kvm_gfn_to_pfn_cache_init(vcpu->kvm,
600 &vcpu->arch.xen.runstate_cache,
601 NULL, KVM_HOST_USES_PFN, data->u.gpa,
602 sizeof(struct vcpu_runstate_info));
605 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_CURRENT:
606 if (!sched_info_on()) {
610 if (data->u.runstate.state > RUNSTATE_offline) {
615 kvm_xen_update_runstate(vcpu, data->u.runstate.state);
619 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_DATA:
620 if (!sched_info_on()) {
624 if (data->u.runstate.state > RUNSTATE_offline) {
628 if (data->u.runstate.state_entry_time !=
629 (data->u.runstate.time_running +
630 data->u.runstate.time_runnable +
631 data->u.runstate.time_blocked +
632 data->u.runstate.time_offline)) {
636 if (get_kvmclock_ns(vcpu->kvm) <
637 data->u.runstate.state_entry_time) {
642 vcpu->arch.xen.current_runstate = data->u.runstate.state;
643 vcpu->arch.xen.runstate_entry_time =
644 data->u.runstate.state_entry_time;
645 vcpu->arch.xen.runstate_times[RUNSTATE_running] =
646 data->u.runstate.time_running;
647 vcpu->arch.xen.runstate_times[RUNSTATE_runnable] =
648 data->u.runstate.time_runnable;
649 vcpu->arch.xen.runstate_times[RUNSTATE_blocked] =
650 data->u.runstate.time_blocked;
651 vcpu->arch.xen.runstate_times[RUNSTATE_offline] =
652 data->u.runstate.time_offline;
653 vcpu->arch.xen.last_steal = current->sched_info.run_delay;
657 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST:
658 if (!sched_info_on()) {
662 if (data->u.runstate.state > RUNSTATE_offline &&
663 data->u.runstate.state != (u64)-1) {
667 /* The adjustment must add up */
668 if (data->u.runstate.state_entry_time !=
669 (data->u.runstate.time_running +
670 data->u.runstate.time_runnable +
671 data->u.runstate.time_blocked +
672 data->u.runstate.time_offline)) {
677 if (get_kvmclock_ns(vcpu->kvm) <
678 (vcpu->arch.xen.runstate_entry_time +
679 data->u.runstate.state_entry_time)) {
684 vcpu->arch.xen.runstate_entry_time +=
685 data->u.runstate.state_entry_time;
686 vcpu->arch.xen.runstate_times[RUNSTATE_running] +=
687 data->u.runstate.time_running;
688 vcpu->arch.xen.runstate_times[RUNSTATE_runnable] +=
689 data->u.runstate.time_runnable;
690 vcpu->arch.xen.runstate_times[RUNSTATE_blocked] +=
691 data->u.runstate.time_blocked;
692 vcpu->arch.xen.runstate_times[RUNSTATE_offline] +=
693 data->u.runstate.time_offline;
695 if (data->u.runstate.state <= RUNSTATE_offline)
696 kvm_xen_update_runstate(vcpu, data->u.runstate.state);
700 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID:
701 if (data->u.vcpu_id >= KVM_MAX_VCPUS)
704 vcpu->arch.xen.vcpu_id = data->u.vcpu_id;
709 case KVM_XEN_VCPU_ATTR_TYPE_TIMER:
710 if (data->u.timer.port) {
711 if (data->u.timer.priority != KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL) {
715 vcpu->arch.xen.timer_virq = data->u.timer.port;
716 kvm_xen_init_timer(vcpu);
718 /* Restart the timer if it's set */
719 if (data->u.timer.expires_ns)
720 kvm_xen_start_timer(vcpu, data->u.timer.expires_ns,
721 data->u.timer.expires_ns -
722 get_kvmclock_ns(vcpu->kvm));
723 } else if (kvm_xen_timer_enabled(vcpu)) {
724 kvm_xen_stop_timer(vcpu);
725 vcpu->arch.xen.timer_virq = 0;
731 case KVM_XEN_VCPU_ATTR_TYPE_UPCALL_VECTOR:
732 if (data->u.vector && data->u.vector < 0x10)
735 vcpu->arch.xen.upcall_vector = data->u.vector;
744 srcu_read_unlock(&vcpu->kvm->srcu, idx);
745 mutex_unlock(&vcpu->kvm->lock);
749 int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data)
753 mutex_lock(&vcpu->kvm->lock);
755 switch (data->type) {
756 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO:
757 if (vcpu->arch.xen.vcpu_info_cache.active)
758 data->u.gpa = vcpu->arch.xen.vcpu_info_cache.gpa;
760 data->u.gpa = GPA_INVALID;
764 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO:
765 if (vcpu->arch.xen.vcpu_time_info_cache.active)
766 data->u.gpa = vcpu->arch.xen.vcpu_time_info_cache.gpa;
768 data->u.gpa = GPA_INVALID;
772 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADDR:
773 if (!sched_info_on()) {
777 if (vcpu->arch.xen.runstate_cache.active) {
778 data->u.gpa = vcpu->arch.xen.runstate_cache.gpa;
783 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_CURRENT:
784 if (!sched_info_on()) {
788 data->u.runstate.state = vcpu->arch.xen.current_runstate;
792 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_DATA:
793 if (!sched_info_on()) {
797 data->u.runstate.state = vcpu->arch.xen.current_runstate;
798 data->u.runstate.state_entry_time =
799 vcpu->arch.xen.runstate_entry_time;
800 data->u.runstate.time_running =
801 vcpu->arch.xen.runstate_times[RUNSTATE_running];
802 data->u.runstate.time_runnable =
803 vcpu->arch.xen.runstate_times[RUNSTATE_runnable];
804 data->u.runstate.time_blocked =
805 vcpu->arch.xen.runstate_times[RUNSTATE_blocked];
806 data->u.runstate.time_offline =
807 vcpu->arch.xen.runstate_times[RUNSTATE_offline];
811 case KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST:
815 case KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID:
816 data->u.vcpu_id = vcpu->arch.xen.vcpu_id;
820 case KVM_XEN_VCPU_ATTR_TYPE_TIMER:
821 data->u.timer.port = vcpu->arch.xen.timer_virq;
822 data->u.timer.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL;
823 data->u.timer.expires_ns = vcpu->arch.xen.timer_expires;
827 case KVM_XEN_VCPU_ATTR_TYPE_UPCALL_VECTOR:
828 data->u.vector = vcpu->arch.xen.upcall_vector;
836 mutex_unlock(&vcpu->kvm->lock);
840 int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data)
842 struct kvm *kvm = vcpu->kvm;
843 u32 page_num = data & ~PAGE_MASK;
844 u64 page_addr = data & PAGE_MASK;
845 bool lm = is_long_mode(vcpu);
847 /* Latch long_mode for shared_info pages etc. */
848 vcpu->kvm->arch.xen.long_mode = lm;
851 * If Xen hypercall intercept is enabled, fill the hypercall
852 * page with VMCALL/VMMCALL instructions since that's what
853 * we catch. Else the VMM has provided the hypercall pages
854 * with instructions of its own choosing, so use those.
856 if (kvm_xen_hypercall_enabled(kvm)) {
863 /* mov imm32, %eax */
864 instructions[0] = 0xb8;
866 /* vmcall / vmmcall */
867 static_call(kvm_x86_patch_hypercall)(vcpu, instructions + 5);
870 instructions[8] = 0xc3;
873 memset(instructions + 9, 0xcc, sizeof(instructions) - 9);
875 for (i = 0; i < PAGE_SIZE / sizeof(instructions); i++) {
876 *(u32 *)&instructions[1] = i;
877 if (kvm_vcpu_write_guest(vcpu,
878 page_addr + (i * sizeof(instructions)),
879 instructions, sizeof(instructions)))
884 * Note, truncation is a non-issue as 'lm' is guaranteed to be
885 * false for a 32-bit kernel, i.e. when hva_t is only 4 bytes.
887 hva_t blob_addr = lm ? kvm->arch.xen_hvm_config.blob_addr_64
888 : kvm->arch.xen_hvm_config.blob_addr_32;
889 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
890 : kvm->arch.xen_hvm_config.blob_size_32;
893 if (page_num >= blob_size)
896 blob_addr += page_num * PAGE_SIZE;
898 page = memdup_user((u8 __user *)blob_addr, PAGE_SIZE);
900 return PTR_ERR(page);
902 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE)) {
910 int kvm_xen_hvm_config(struct kvm *kvm, struct kvm_xen_hvm_config *xhc)
912 /* Only some feature flags need to be *enabled* by userspace */
913 u32 permitted_flags = KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL |
914 KVM_XEN_HVM_CONFIG_EVTCHN_SEND;
916 if (xhc->flags & ~permitted_flags)
920 * With hypercall interception the kernel generates its own
921 * hypercall page so it must not be provided.
923 if ((xhc->flags & KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL) &&
924 (xhc->blob_addr_32 || xhc->blob_addr_64 ||
925 xhc->blob_size_32 || xhc->blob_size_64))
928 mutex_lock(&kvm->lock);
930 if (xhc->msr && !kvm->arch.xen_hvm_config.msr)
931 static_branch_inc(&kvm_xen_enabled.key);
932 else if (!xhc->msr && kvm->arch.xen_hvm_config.msr)
933 static_branch_slow_dec_deferred(&kvm_xen_enabled);
935 memcpy(&kvm->arch.xen_hvm_config, xhc, sizeof(*xhc));
937 mutex_unlock(&kvm->lock);
941 static int kvm_xen_hypercall_set_result(struct kvm_vcpu *vcpu, u64 result)
943 kvm_rax_write(vcpu, result);
944 return kvm_skip_emulated_instruction(vcpu);
947 static int kvm_xen_hypercall_complete_userspace(struct kvm_vcpu *vcpu)
949 struct kvm_run *run = vcpu->run;
951 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.xen.hypercall_rip)))
954 return kvm_xen_hypercall_set_result(vcpu, run->xen.u.hcall.result);
957 static bool wait_pending_event(struct kvm_vcpu *vcpu, int nr_ports,
958 evtchn_port_t *ports)
960 struct kvm *kvm = vcpu->kvm;
961 struct gfn_to_pfn_cache *gpc = &kvm->arch.xen.shinfo_cache;
962 unsigned long *pending_bits;
967 read_lock_irqsave(&gpc->lock, flags);
968 idx = srcu_read_lock(&kvm->srcu);
969 if (!kvm_gfn_to_pfn_cache_check(kvm, gpc, gpc->gpa, PAGE_SIZE))
973 if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) {
974 struct shared_info *shinfo = gpc->khva;
975 pending_bits = (unsigned long *)&shinfo->evtchn_pending;
977 struct compat_shared_info *shinfo = gpc->khva;
978 pending_bits = (unsigned long *)&shinfo->evtchn_pending;
981 for (i = 0; i < nr_ports; i++) {
982 if (test_bit(ports[i], pending_bits)) {
989 srcu_read_unlock(&kvm->srcu, idx);
990 read_unlock_irqrestore(&gpc->lock, flags);
995 static bool kvm_xen_schedop_poll(struct kvm_vcpu *vcpu, bool longmode,
999 struct sched_poll sched_poll;
1000 evtchn_port_t port, *ports;
1003 if (!longmode || !lapic_in_kernel(vcpu) ||
1004 !(vcpu->kvm->arch.xen_hvm_config.flags & KVM_XEN_HVM_CONFIG_EVTCHN_SEND))
1007 idx = srcu_read_lock(&vcpu->kvm->srcu);
1008 gpa = kvm_mmu_gva_to_gpa_system(vcpu, param, NULL);
1009 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1011 if (!gpa || kvm_vcpu_read_guest(vcpu, gpa, &sched_poll,
1012 sizeof(sched_poll))) {
1017 if (unlikely(sched_poll.nr_ports > 1)) {
1018 /* Xen (unofficially) limits number of pollers to 128 */
1019 if (sched_poll.nr_ports > 128) {
1024 ports = kmalloc_array(sched_poll.nr_ports,
1025 sizeof(*ports), GFP_KERNEL);
1033 for (i = 0; i < sched_poll.nr_ports; i++) {
1034 idx = srcu_read_lock(&vcpu->kvm->srcu);
1035 gpa = kvm_mmu_gva_to_gpa_system(vcpu,
1036 (gva_t)(sched_poll.ports + i),
1038 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1040 if (!gpa || kvm_vcpu_read_guest(vcpu, gpa,
1041 &ports[i], sizeof(port))) {
1047 if (sched_poll.nr_ports == 1)
1048 vcpu->arch.xen.poll_evtchn = port;
1050 vcpu->arch.xen.poll_evtchn = -1;
1052 set_bit(vcpu->vcpu_idx, vcpu->kvm->arch.xen.poll_mask);
1054 if (!wait_pending_event(vcpu, sched_poll.nr_ports, ports)) {
1055 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
1057 if (sched_poll.timeout)
1058 mod_timer(&vcpu->arch.xen.poll_timer,
1059 jiffies + nsecs_to_jiffies(sched_poll.timeout));
1061 kvm_vcpu_halt(vcpu);
1063 if (sched_poll.timeout)
1064 del_timer(&vcpu->arch.xen.poll_timer);
1066 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
1067 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
1070 vcpu->arch.xen.poll_evtchn = 0;
1073 /* Really, this is only needed in case of timeout */
1074 clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.xen.poll_mask);
1076 if (unlikely(sched_poll.nr_ports > 1))
1081 static void cancel_evtchn_poll(struct timer_list *t)
1083 struct kvm_vcpu *vcpu = from_timer(vcpu, t, arch.xen.poll_timer);
1085 kvm_make_request(KVM_REQ_UNBLOCK, vcpu);
1086 kvm_vcpu_kick(vcpu);
1089 static bool kvm_xen_hcall_sched_op(struct kvm_vcpu *vcpu, bool longmode,
1090 int cmd, u64 param, u64 *r)
1094 if (kvm_xen_schedop_poll(vcpu, longmode, param, r))
1098 kvm_vcpu_on_spin(vcpu, true);
1108 struct compat_vcpu_set_singleshot_timer {
1109 uint64_t timeout_abs_ns;
1111 } __attribute__((packed));
1113 static bool kvm_xen_hcall_vcpu_op(struct kvm_vcpu *vcpu, bool longmode, int cmd,
1114 int vcpu_id, u64 param, u64 *r)
1116 struct vcpu_set_singleshot_timer oneshot;
1121 if (!kvm_xen_timer_enabled(vcpu))
1125 case VCPUOP_set_singleshot_timer:
1126 if (vcpu->arch.xen.vcpu_id != vcpu_id) {
1130 idx = srcu_read_lock(&vcpu->kvm->srcu);
1131 gpa = kvm_mmu_gva_to_gpa_system(vcpu, param, NULL);
1132 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1135 * The only difference for 32-bit compat is the 4 bytes of
1136 * padding after the interesting part of the structure. So
1137 * for a faithful emulation of Xen we have to *try* to copy
1138 * the padding and return -EFAULT if we can't. Otherwise we
1139 * might as well just have copied the 12-byte 32-bit struct.
1141 BUILD_BUG_ON(offsetof(struct compat_vcpu_set_singleshot_timer, timeout_abs_ns) !=
1142 offsetof(struct vcpu_set_singleshot_timer, timeout_abs_ns));
1143 BUILD_BUG_ON(sizeof_field(struct compat_vcpu_set_singleshot_timer, timeout_abs_ns) !=
1144 sizeof_field(struct vcpu_set_singleshot_timer, timeout_abs_ns));
1145 BUILD_BUG_ON(offsetof(struct compat_vcpu_set_singleshot_timer, flags) !=
1146 offsetof(struct vcpu_set_singleshot_timer, flags));
1147 BUILD_BUG_ON(sizeof_field(struct compat_vcpu_set_singleshot_timer, flags) !=
1148 sizeof_field(struct vcpu_set_singleshot_timer, flags));
1151 kvm_vcpu_read_guest(vcpu, gpa, &oneshot, longmode ? sizeof(oneshot) :
1152 sizeof(struct compat_vcpu_set_singleshot_timer))) {
1157 delta = oneshot.timeout_abs_ns - get_kvmclock_ns(vcpu->kvm);
1158 if ((oneshot.flags & VCPU_SSHOTTMR_future) && delta < 0) {
1163 kvm_xen_start_timer(vcpu, oneshot.timeout_abs_ns, delta);
1167 case VCPUOP_stop_singleshot_timer:
1168 if (vcpu->arch.xen.vcpu_id != vcpu_id) {
1172 kvm_xen_stop_timer(vcpu);
1180 static bool kvm_xen_hcall_set_timer_op(struct kvm_vcpu *vcpu, uint64_t timeout,
1183 if (!kvm_xen_timer_enabled(vcpu))
1187 uint64_t guest_now = get_kvmclock_ns(vcpu->kvm);
1188 int64_t delta = timeout - guest_now;
1190 /* Xen has a 'Linux workaround' in do_set_timer_op() which
1191 * checks for negative absolute timeout values (caused by
1192 * integer overflow), and for values about 13 days in the
1193 * future (2^50ns) which would be caused by jiffies
1194 * overflow. For those cases, it sets the timeout 100ms in
1195 * the future (not *too* soon, since if a guest really did
1196 * set a long timeout on purpose we don't want to keep
1197 * churning CPU time by waking it up).
1199 if (unlikely((int64_t)timeout < 0 ||
1200 (delta > 0 && (uint32_t) (delta >> 50) != 0))) {
1201 delta = 100 * NSEC_PER_MSEC;
1202 timeout = guest_now + delta;
1205 kvm_xen_start_timer(vcpu, timeout, delta);
1207 kvm_xen_stop_timer(vcpu);
1214 int kvm_xen_hypercall(struct kvm_vcpu *vcpu)
1217 u64 input, params[6], r = -ENOSYS;
1218 bool handled = false;
1220 input = (u64)kvm_register_read(vcpu, VCPU_REGS_RAX);
1222 /* Hyper-V hypercalls get bit 31 set in EAX */
1223 if ((input & 0x80000000) &&
1224 kvm_hv_hypercall_enabled(vcpu))
1225 return kvm_hv_hypercall(vcpu);
1227 longmode = is_64_bit_hypercall(vcpu);
1229 params[0] = (u32)kvm_rbx_read(vcpu);
1230 params[1] = (u32)kvm_rcx_read(vcpu);
1231 params[2] = (u32)kvm_rdx_read(vcpu);
1232 params[3] = (u32)kvm_rsi_read(vcpu);
1233 params[4] = (u32)kvm_rdi_read(vcpu);
1234 params[5] = (u32)kvm_rbp_read(vcpu);
1236 #ifdef CONFIG_X86_64
1238 params[0] = (u64)kvm_rdi_read(vcpu);
1239 params[1] = (u64)kvm_rsi_read(vcpu);
1240 params[2] = (u64)kvm_rdx_read(vcpu);
1241 params[3] = (u64)kvm_r10_read(vcpu);
1242 params[4] = (u64)kvm_r8_read(vcpu);
1243 params[5] = (u64)kvm_r9_read(vcpu);
1246 trace_kvm_xen_hypercall(input, params[0], params[1], params[2],
1247 params[3], params[4], params[5]);
1250 case __HYPERVISOR_xen_version:
1251 if (params[0] == XENVER_version && vcpu->kvm->arch.xen.xen_version) {
1252 r = vcpu->kvm->arch.xen.xen_version;
1256 case __HYPERVISOR_event_channel_op:
1257 if (params[0] == EVTCHNOP_send)
1258 handled = kvm_xen_hcall_evtchn_send(vcpu, params[1], &r);
1260 case __HYPERVISOR_sched_op:
1261 handled = kvm_xen_hcall_sched_op(vcpu, longmode, params[0],
1264 case __HYPERVISOR_vcpu_op:
1265 handled = kvm_xen_hcall_vcpu_op(vcpu, longmode, params[0], params[1],
1268 case __HYPERVISOR_set_timer_op: {
1269 u64 timeout = params[0];
1270 /* In 32-bit mode, the 64-bit timeout is in two 32-bit params. */
1272 timeout |= params[1] << 32;
1273 handled = kvm_xen_hcall_set_timer_op(vcpu, timeout, &r);
1281 return kvm_xen_hypercall_set_result(vcpu, r);
1283 vcpu->run->exit_reason = KVM_EXIT_XEN;
1284 vcpu->run->xen.type = KVM_EXIT_XEN_HCALL;
1285 vcpu->run->xen.u.hcall.longmode = longmode;
1286 vcpu->run->xen.u.hcall.cpl = static_call(kvm_x86_get_cpl)(vcpu);
1287 vcpu->run->xen.u.hcall.input = input;
1288 vcpu->run->xen.u.hcall.params[0] = params[0];
1289 vcpu->run->xen.u.hcall.params[1] = params[1];
1290 vcpu->run->xen.u.hcall.params[2] = params[2];
1291 vcpu->run->xen.u.hcall.params[3] = params[3];
1292 vcpu->run->xen.u.hcall.params[4] = params[4];
1293 vcpu->run->xen.u.hcall.params[5] = params[5];
1294 vcpu->arch.xen.hypercall_rip = kvm_get_linear_rip(vcpu);
1295 vcpu->arch.complete_userspace_io =
1296 kvm_xen_hypercall_complete_userspace;
1301 static inline int max_evtchn_port(struct kvm *kvm)
1303 if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode)
1304 return EVTCHN_2L_NR_CHANNELS;
1306 return COMPAT_EVTCHN_2L_NR_CHANNELS;
1309 static void kvm_xen_check_poller(struct kvm_vcpu *vcpu, int port)
1311 int poll_evtchn = vcpu->arch.xen.poll_evtchn;
1313 if ((poll_evtchn == port || poll_evtchn == -1) &&
1314 test_and_clear_bit(vcpu->vcpu_idx, vcpu->kvm->arch.xen.poll_mask)) {
1315 kvm_make_request(KVM_REQ_UNBLOCK, vcpu);
1316 kvm_vcpu_kick(vcpu);
1321 * The return value from this function is propagated to kvm_set_irq() API,
1323 * < 0 Interrupt was ignored (masked or not delivered for other reasons)
1324 * = 0 Interrupt was coalesced (previous irq is still pending)
1325 * > 0 Number of CPUs interrupt was delivered to
1327 * It is also called directly from kvm_arch_set_irq_inatomic(), where the
1328 * only check on its return value is a comparison with -EWOULDBLOCK'.
1330 int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe, struct kvm *kvm)
1332 struct gfn_to_pfn_cache *gpc = &kvm->arch.xen.shinfo_cache;
1333 struct kvm_vcpu *vcpu;
1334 unsigned long *pending_bits, *mask_bits;
1335 unsigned long flags;
1337 bool kick_vcpu = false;
1338 int vcpu_idx, idx, rc;
1340 vcpu_idx = READ_ONCE(xe->vcpu_idx);
1342 vcpu = kvm_get_vcpu(kvm, vcpu_idx);
1344 vcpu = kvm_get_vcpu_by_id(kvm, xe->vcpu_id);
1347 WRITE_ONCE(xe->vcpu_idx, vcpu->vcpu_idx);
1350 if (!vcpu->arch.xen.vcpu_info_cache.active)
1353 if (xe->port >= max_evtchn_port(kvm))
1358 idx = srcu_read_lock(&kvm->srcu);
1360 read_lock_irqsave(&gpc->lock, flags);
1361 if (!kvm_gfn_to_pfn_cache_check(kvm, gpc, gpc->gpa, PAGE_SIZE))
1364 if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) {
1365 struct shared_info *shinfo = gpc->khva;
1366 pending_bits = (unsigned long *)&shinfo->evtchn_pending;
1367 mask_bits = (unsigned long *)&shinfo->evtchn_mask;
1368 port_word_bit = xe->port / 64;
1370 struct compat_shared_info *shinfo = gpc->khva;
1371 pending_bits = (unsigned long *)&shinfo->evtchn_pending;
1372 mask_bits = (unsigned long *)&shinfo->evtchn_mask;
1373 port_word_bit = xe->port / 32;
1377 * If this port wasn't already set, and if it isn't masked, then
1378 * we try to set the corresponding bit in the in-kernel shadow of
1379 * evtchn_pending_sel for the target vCPU. And if *that* wasn't
1380 * already set, then we kick the vCPU in question to write to the
1381 * *real* evtchn_pending_sel in its own guest vcpu_info struct.
1383 if (test_and_set_bit(xe->port, pending_bits)) {
1384 rc = 0; /* It was already raised */
1385 } else if (test_bit(xe->port, mask_bits)) {
1386 rc = -ENOTCONN; /* Masked */
1387 kvm_xen_check_poller(vcpu, xe->port);
1389 rc = 1; /* Delivered to the bitmap in shared_info. */
1390 /* Now switch to the vCPU's vcpu_info to set the index and pending_sel */
1391 read_unlock_irqrestore(&gpc->lock, flags);
1392 gpc = &vcpu->arch.xen.vcpu_info_cache;
1394 read_lock_irqsave(&gpc->lock, flags);
1395 if (!kvm_gfn_to_pfn_cache_check(kvm, gpc, gpc->gpa, sizeof(struct vcpu_info))) {
1397 * Could not access the vcpu_info. Set the bit in-kernel
1398 * and prod the vCPU to deliver it for itself.
1400 if (!test_and_set_bit(port_word_bit, &vcpu->arch.xen.evtchn_pending_sel))
1405 if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) {
1406 struct vcpu_info *vcpu_info = gpc->khva;
1407 if (!test_and_set_bit(port_word_bit, &vcpu_info->evtchn_pending_sel)) {
1408 WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1);
1412 struct compat_vcpu_info *vcpu_info = gpc->khva;
1413 if (!test_and_set_bit(port_word_bit,
1414 (unsigned long *)&vcpu_info->evtchn_pending_sel)) {
1415 WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1);
1420 /* For the per-vCPU lapic vector, deliver it as MSI. */
1421 if (kick_vcpu && vcpu->arch.xen.upcall_vector) {
1422 kvm_xen_inject_vcpu_vector(vcpu);
1428 read_unlock_irqrestore(&gpc->lock, flags);
1429 srcu_read_unlock(&kvm->srcu, idx);
1432 kvm_make_request(KVM_REQ_UNBLOCK, vcpu);
1433 kvm_vcpu_kick(vcpu);
1439 static int kvm_xen_set_evtchn(struct kvm_xen_evtchn *xe, struct kvm *kvm)
1441 bool mm_borrowed = false;
1444 rc = kvm_xen_set_evtchn_fast(xe, kvm);
1445 if (rc != -EWOULDBLOCK)
1448 if (current->mm != kvm->mm) {
1450 * If not on a thread which already belongs to this KVM,
1451 * we'd better be in the irqfd workqueue.
1453 if (WARN_ON_ONCE(current->mm))
1456 kthread_use_mm(kvm->mm);
1461 * For the irqfd workqueue, using the main kvm->lock mutex is
1462 * fine since this function is invoked from kvm_set_irq() with
1463 * no other lock held, no srcu. In future if it will be called
1464 * directly from a vCPU thread (e.g. on hypercall for an IPI)
1465 * then it may need to switch to using a leaf-node mutex for
1466 * serializing the shared_info mapping.
1468 mutex_lock(&kvm->lock);
1471 * It is theoretically possible for the page to be unmapped
1472 * and the MMU notifier to invalidate the shared_info before
1473 * we even get to use it. In that case, this looks like an
1474 * infinite loop. It was tempting to do it via the userspace
1475 * HVA instead... but that just *hides* the fact that it's
1476 * an infinite loop, because if a fault occurs and it waits
1477 * for the page to come back, it can *still* immediately
1478 * fault and have to wait again, repeatedly.
1480 * Conversely, the page could also have been reinstated by
1481 * another thread before we even obtain the mutex above, so
1482 * check again *first* before remapping it.
1485 struct gfn_to_pfn_cache *gpc = &kvm->arch.xen.shinfo_cache;
1488 rc = kvm_xen_set_evtchn_fast(xe, kvm);
1489 if (rc != -EWOULDBLOCK)
1492 idx = srcu_read_lock(&kvm->srcu);
1493 rc = kvm_gfn_to_pfn_cache_refresh(kvm, gpc, gpc->gpa, PAGE_SIZE);
1494 srcu_read_unlock(&kvm->srcu, idx);
1497 mutex_unlock(&kvm->lock);
1500 kthread_unuse_mm(kvm->mm);
1505 /* This is the version called from kvm_set_irq() as the .set function */
1506 static int evtchn_set_fn(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm,
1507 int irq_source_id, int level, bool line_status)
1512 return kvm_xen_set_evtchn(&e->xen_evtchn, kvm);
1516 * Set up an event channel interrupt from the KVM IRQ routing table.
1517 * Used for e.g. PIRQ from passed through physical devices.
1519 int kvm_xen_setup_evtchn(struct kvm *kvm,
1520 struct kvm_kernel_irq_routing_entry *e,
1521 const struct kvm_irq_routing_entry *ue)
1524 struct kvm_vcpu *vcpu;
1526 if (ue->u.xen_evtchn.port >= max_evtchn_port(kvm))
1529 /* We only support 2 level event channels for now */
1530 if (ue->u.xen_evtchn.priority != KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL)
1534 * Xen gives us interesting mappings from vCPU index to APIC ID,
1535 * which means kvm_get_vcpu_by_id() has to iterate over all vCPUs
1536 * to find it. Do that once at setup time, instead of every time.
1537 * But beware that on live update / live migration, the routing
1538 * table might be reinstated before the vCPU threads have finished
1539 * recreating their vCPUs.
1541 vcpu = kvm_get_vcpu_by_id(kvm, ue->u.xen_evtchn.vcpu);
1543 e->xen_evtchn.vcpu_idx = vcpu->vcpu_idx;
1545 e->xen_evtchn.vcpu_idx = -1;
1547 e->xen_evtchn.port = ue->u.xen_evtchn.port;
1548 e->xen_evtchn.vcpu_id = ue->u.xen_evtchn.vcpu;
1549 e->xen_evtchn.priority = ue->u.xen_evtchn.priority;
1550 e->set = evtchn_set_fn;
1556 * Explicit event sending from userspace with KVM_XEN_HVM_EVTCHN_SEND ioctl.
1558 int kvm_xen_hvm_evtchn_send(struct kvm *kvm, struct kvm_irq_routing_xen_evtchn *uxe)
1560 struct kvm_xen_evtchn e;
1563 if (!uxe->port || uxe->port >= max_evtchn_port(kvm))
1566 /* We only support 2 level event channels for now */
1567 if (uxe->priority != KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL)
1571 e.vcpu_id = uxe->vcpu;
1573 e.priority = uxe->priority;
1575 ret = kvm_xen_set_evtchn(&e, kvm);
1578 * None of that 'return 1 if it actually got delivered' nonsense.
1579 * We don't care if it was masked (-ENOTCONN) either.
1581 if (ret > 0 || ret == -ENOTCONN)
1588 * Support for *outbound* event channel events via the EVTCHNOP_send hypercall.
1594 struct kvm_xen_evtchn port;
1596 u32 port; /* zero */
1597 struct eventfd_ctx *ctx;
1603 * Update target vCPU or priority for a registered sending channel.
1605 static int kvm_xen_eventfd_update(struct kvm *kvm,
1606 struct kvm_xen_hvm_attr *data)
1608 u32 port = data->u.evtchn.send_port;
1609 struct evtchnfd *evtchnfd;
1611 if (!port || port >= max_evtchn_port(kvm))
1614 mutex_lock(&kvm->lock);
1615 evtchnfd = idr_find(&kvm->arch.xen.evtchn_ports, port);
1616 mutex_unlock(&kvm->lock);
1621 /* For an UPDATE, nothing may change except the priority/vcpu */
1622 if (evtchnfd->type != data->u.evtchn.type)
1626 * Port cannot change, and if it's zero that was an eventfd
1627 * which can't be changed either.
1629 if (!evtchnfd->deliver.port.port ||
1630 evtchnfd->deliver.port.port != data->u.evtchn.deliver.port.port)
1633 /* We only support 2 level event channels for now */
1634 if (data->u.evtchn.deliver.port.priority != KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL)
1637 mutex_lock(&kvm->lock);
1638 evtchnfd->deliver.port.priority = data->u.evtchn.deliver.port.priority;
1639 if (evtchnfd->deliver.port.vcpu_id != data->u.evtchn.deliver.port.vcpu) {
1640 evtchnfd->deliver.port.vcpu_id = data->u.evtchn.deliver.port.vcpu;
1641 evtchnfd->deliver.port.vcpu_idx = -1;
1643 mutex_unlock(&kvm->lock);
1648 * Configure the target (eventfd or local port delivery) for sending on
1649 * a given event channel.
1651 static int kvm_xen_eventfd_assign(struct kvm *kvm,
1652 struct kvm_xen_hvm_attr *data)
1654 u32 port = data->u.evtchn.send_port;
1655 struct eventfd_ctx *eventfd = NULL;
1656 struct evtchnfd *evtchnfd = NULL;
1659 if (!port || port >= max_evtchn_port(kvm))
1662 evtchnfd = kzalloc(sizeof(struct evtchnfd), GFP_KERNEL);
1666 switch(data->u.evtchn.type) {
1667 case EVTCHNSTAT_ipi:
1668 /* IPI must map back to the same port# */
1669 if (data->u.evtchn.deliver.port.port != data->u.evtchn.send_port)
1670 goto out; /* -EINVAL */
1673 case EVTCHNSTAT_interdomain:
1674 if (data->u.evtchn.deliver.port.port) {
1675 if (data->u.evtchn.deliver.port.port >= max_evtchn_port(kvm))
1676 goto out; /* -EINVAL */
1678 eventfd = eventfd_ctx_fdget(data->u.evtchn.deliver.eventfd.fd);
1679 if (IS_ERR(eventfd)) {
1680 ret = PTR_ERR(eventfd);
1686 case EVTCHNSTAT_virq:
1687 case EVTCHNSTAT_closed:
1688 case EVTCHNSTAT_unbound:
1689 case EVTCHNSTAT_pirq:
1690 default: /* Unknown event channel type */
1691 goto out; /* -EINVAL */
1694 evtchnfd->send_port = data->u.evtchn.send_port;
1695 evtchnfd->type = data->u.evtchn.type;
1697 evtchnfd->deliver.eventfd.ctx = eventfd;
1699 /* We only support 2 level event channels for now */
1700 if (data->u.evtchn.deliver.port.priority != KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL)
1701 goto out; /* -EINVAL; */
1703 evtchnfd->deliver.port.port = data->u.evtchn.deliver.port.port;
1704 evtchnfd->deliver.port.vcpu_id = data->u.evtchn.deliver.port.vcpu;
1705 evtchnfd->deliver.port.vcpu_idx = -1;
1706 evtchnfd->deliver.port.priority = data->u.evtchn.deliver.port.priority;
1709 mutex_lock(&kvm->lock);
1710 ret = idr_alloc(&kvm->arch.xen.evtchn_ports, evtchnfd, port, port + 1,
1712 mutex_unlock(&kvm->lock);
1720 eventfd_ctx_put(eventfd);
1725 static int kvm_xen_eventfd_deassign(struct kvm *kvm, u32 port)
1727 struct evtchnfd *evtchnfd;
1729 mutex_lock(&kvm->lock);
1730 evtchnfd = idr_remove(&kvm->arch.xen.evtchn_ports, port);
1731 mutex_unlock(&kvm->lock);
1737 synchronize_srcu(&kvm->srcu);
1738 if (!evtchnfd->deliver.port.port)
1739 eventfd_ctx_put(evtchnfd->deliver.eventfd.ctx);
1744 static int kvm_xen_eventfd_reset(struct kvm *kvm)
1746 struct evtchnfd *evtchnfd;
1749 mutex_lock(&kvm->lock);
1750 idr_for_each_entry(&kvm->arch.xen.evtchn_ports, evtchnfd, i) {
1751 idr_remove(&kvm->arch.xen.evtchn_ports, evtchnfd->send_port);
1752 synchronize_srcu(&kvm->srcu);
1753 if (!evtchnfd->deliver.port.port)
1754 eventfd_ctx_put(evtchnfd->deliver.eventfd.ctx);
1757 mutex_unlock(&kvm->lock);
1762 static int kvm_xen_setattr_evtchn(struct kvm *kvm, struct kvm_xen_hvm_attr *data)
1764 u32 port = data->u.evtchn.send_port;
1766 if (data->u.evtchn.flags == KVM_XEN_EVTCHN_RESET)
1767 return kvm_xen_eventfd_reset(kvm);
1769 if (!port || port >= max_evtchn_port(kvm))
1772 if (data->u.evtchn.flags == KVM_XEN_EVTCHN_DEASSIGN)
1773 return kvm_xen_eventfd_deassign(kvm, port);
1774 if (data->u.evtchn.flags == KVM_XEN_EVTCHN_UPDATE)
1775 return kvm_xen_eventfd_update(kvm, data);
1776 if (data->u.evtchn.flags)
1779 return kvm_xen_eventfd_assign(kvm, data);
1782 static bool kvm_xen_hcall_evtchn_send(struct kvm_vcpu *vcpu, u64 param, u64 *r)
1784 struct evtchnfd *evtchnfd;
1785 struct evtchn_send send;
1789 idx = srcu_read_lock(&vcpu->kvm->srcu);
1790 gpa = kvm_mmu_gva_to_gpa_system(vcpu, param, NULL);
1791 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1793 if (!gpa || kvm_vcpu_read_guest(vcpu, gpa, &send, sizeof(send))) {
1798 /* The evtchn_ports idr is protected by vcpu->kvm->srcu */
1799 evtchnfd = idr_find(&vcpu->kvm->arch.xen.evtchn_ports, send.port);
1803 if (evtchnfd->deliver.port.port) {
1804 int ret = kvm_xen_set_evtchn(&evtchnfd->deliver.port, vcpu->kvm);
1805 if (ret < 0 && ret != -ENOTCONN)
1808 eventfd_signal(evtchnfd->deliver.eventfd.ctx, 1);
1815 void kvm_xen_init_vcpu(struct kvm_vcpu *vcpu)
1817 vcpu->arch.xen.vcpu_id = vcpu->vcpu_idx;
1818 vcpu->arch.xen.poll_evtchn = 0;
1819 timer_setup(&vcpu->arch.xen.poll_timer, cancel_evtchn_poll, 0);
1822 void kvm_xen_destroy_vcpu(struct kvm_vcpu *vcpu)
1824 if (kvm_xen_timer_enabled(vcpu))
1825 kvm_xen_stop_timer(vcpu);
1827 kvm_gfn_to_pfn_cache_destroy(vcpu->kvm,
1828 &vcpu->arch.xen.runstate_cache);
1829 kvm_gfn_to_pfn_cache_destroy(vcpu->kvm,
1830 &vcpu->arch.xen.vcpu_info_cache);
1831 kvm_gfn_to_pfn_cache_destroy(vcpu->kvm,
1832 &vcpu->arch.xen.vcpu_time_info_cache);
1833 del_timer_sync(&vcpu->arch.xen.poll_timer);
1836 void kvm_xen_init_vm(struct kvm *kvm)
1838 idr_init(&kvm->arch.xen.evtchn_ports);
1841 void kvm_xen_destroy_vm(struct kvm *kvm)
1843 struct evtchnfd *evtchnfd;
1846 kvm_gfn_to_pfn_cache_destroy(kvm, &kvm->arch.xen.shinfo_cache);
1848 idr_for_each_entry(&kvm->arch.xen.evtchn_ports, evtchnfd, i) {
1849 if (!evtchnfd->deliver.port.port)
1850 eventfd_ctx_put(evtchnfd->deliver.eventfd.ctx);
1853 idr_destroy(&kvm->arch.xen.evtchn_ports);
1855 if (kvm->arch.xen_hvm_config.msr)
1856 static_branch_slow_dec_deferred(&kvm_xen_enabled);