xref: /openbmc/qemu/hw/i386/kvm/xen_evtchn.c (revision 507cb64d)
1 /*
2  * QEMU Xen emulation: Event channel support
3  *
4  * Copyright © 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5  *
6  * Authors: David Woodhouse <dwmw2@infradead.org>
7  *
8  * This work is licensed under the terms of the GNU GPL, version 2 or later.
9  * See the COPYING file in the top-level directory.
10  */
11 
12 #include "qemu/osdep.h"
13 #include "qemu/host-utils.h"
14 #include "qemu/module.h"
15 #include "qemu/lockable.h"
16 #include "qemu/main-loop.h"
17 #include "qemu/log.h"
18 #include "monitor/monitor.h"
19 #include "monitor/hmp.h"
20 #include "qapi/error.h"
21 #include "qapi/qapi-commands-misc-target.h"
22 #include "qapi/qmp/qdict.h"
23 #include "qom/object.h"
24 #include "exec/target_page.h"
25 #include "exec/address-spaces.h"
26 #include "migration/vmstate.h"
27 
28 #include "hw/sysbus.h"
29 #include "hw/xen/xen.h"
30 
31 #include "xen_evtchn.h"
32 #include "xen_overlay.h"
33 
34 #include "sysemu/kvm.h"
35 #include "sysemu/kvm_xen.h"
36 #include <linux/kvm.h>
37 
38 #include "hw/xen/interface/memory.h"
39 #include "hw/xen/interface/hvm/params.h"
40 
41 #define TYPE_XEN_EVTCHN "xen-evtchn"
42 OBJECT_DECLARE_SIMPLE_TYPE(XenEvtchnState, XEN_EVTCHN)
43 
44 typedef struct XenEvtchnPort {
45     uint32_t vcpu;      /* Xen/ACPI vcpu_id */
46     uint16_t type;      /* EVTCHNSTAT_xxxx */
47     uint16_t type_val;  /* pirq# / virq# / remote port according to type */
48 } XenEvtchnPort;
49 
50 /* 32-bit compatibility definitions, also used natively in 32-bit build */
51 struct compat_arch_vcpu_info {
52     unsigned int cr2;
53     unsigned int pad[5];
54 };
55 
56 struct compat_vcpu_info {
57     uint8_t evtchn_upcall_pending;
58     uint8_t evtchn_upcall_mask;
59     uint16_t pad;
60     uint32_t evtchn_pending_sel;
61     struct compat_arch_vcpu_info arch;
62     struct vcpu_time_info time;
63 }; /* 64 bytes (x86) */
64 
65 struct compat_arch_shared_info {
66     unsigned int max_pfn;
67     unsigned int pfn_to_mfn_frame_list_list;
68     unsigned int nmi_reason;
69     unsigned int p2m_cr3;
70     unsigned int p2m_vaddr;
71     unsigned int p2m_generation;
72     uint32_t wc_sec_hi;
73 };
74 
75 struct compat_shared_info {
76     struct compat_vcpu_info vcpu_info[XEN_LEGACY_MAX_VCPUS];
77     uint32_t evtchn_pending[32];
78     uint32_t evtchn_mask[32];
79     uint32_t wc_version;      /* Version counter: see vcpu_time_info_t. */
80     uint32_t wc_sec;
81     uint32_t wc_nsec;
82     struct compat_arch_shared_info arch;
83 };
84 
85 #define COMPAT_EVTCHN_2L_NR_CHANNELS            1024
86 
87 /*
88  * For unbound/interdomain ports there are only two possible remote
89  * domains; self and QEMU. Use a single high bit in type_val for that,
90  * and the low bits for the remote port number (or 0 for unbound).
91  */
92 #define PORT_INFO_TYPEVAL_REMOTE_QEMU           0x8000
93 #define PORT_INFO_TYPEVAL_REMOTE_PORT_MASK      0x7FFF
94 
95 struct XenEvtchnState {
96     /*< private >*/
97     SysBusDevice busdev;
98     /*< public >*/
99 
100     uint64_t callback_param;
101     bool evtchn_in_kernel;
102 
103     QemuMutex port_lock;
104     uint32_t nr_ports;
105     XenEvtchnPort port_table[EVTCHN_2L_NR_CHANNELS];
106 };
107 
108 struct XenEvtchnState *xen_evtchn_singleton;
109 
110 /* Top bits of callback_param are the type (HVM_PARAM_CALLBACK_TYPE_xxx) */
111 #define CALLBACK_VIA_TYPE_SHIFT 56
112 
113 static int xen_evtchn_post_load(void *opaque, int version_id)
114 {
115     XenEvtchnState *s = opaque;
116 
117     if (s->callback_param) {
118         xen_evtchn_set_callback_param(s->callback_param);
119     }
120 
121     return 0;
122 }
123 
124 static bool xen_evtchn_is_needed(void *opaque)
125 {
126     return xen_mode == XEN_EMULATE;
127 }
128 
129 static const VMStateDescription xen_evtchn_port_vmstate = {
130     .name = "xen_evtchn_port",
131     .version_id = 1,
132     .minimum_version_id = 1,
133     .fields = (VMStateField[]) {
134         VMSTATE_UINT32(vcpu, XenEvtchnPort),
135         VMSTATE_UINT16(type, XenEvtchnPort),
136         VMSTATE_UINT16(type_val, XenEvtchnPort),
137         VMSTATE_END_OF_LIST()
138     }
139 };
140 
141 static const VMStateDescription xen_evtchn_vmstate = {
142     .name = "xen_evtchn",
143     .version_id = 1,
144     .minimum_version_id = 1,
145     .needed = xen_evtchn_is_needed,
146     .post_load = xen_evtchn_post_load,
147     .fields = (VMStateField[]) {
148         VMSTATE_UINT64(callback_param, XenEvtchnState),
149         VMSTATE_UINT32(nr_ports, XenEvtchnState),
150         VMSTATE_STRUCT_VARRAY_UINT32(port_table, XenEvtchnState, nr_ports, 1,
151                                      xen_evtchn_port_vmstate, XenEvtchnPort),
152         VMSTATE_END_OF_LIST()
153     }
154 };
155 
156 static void xen_evtchn_class_init(ObjectClass *klass, void *data)
157 {
158     DeviceClass *dc = DEVICE_CLASS(klass);
159 
160     dc->vmsd = &xen_evtchn_vmstate;
161 }
162 
163 static const TypeInfo xen_evtchn_info = {
164     .name          = TYPE_XEN_EVTCHN,
165     .parent        = TYPE_SYS_BUS_DEVICE,
166     .instance_size = sizeof(XenEvtchnState),
167     .class_init    = xen_evtchn_class_init,
168 };
169 
170 void xen_evtchn_create(void)
171 {
172     XenEvtchnState *s = XEN_EVTCHN(sysbus_create_simple(TYPE_XEN_EVTCHN,
173                                                         -1, NULL));
174     xen_evtchn_singleton = s;
175 
176     qemu_mutex_init(&s->port_lock);
177 }
178 
179 static void xen_evtchn_register_types(void)
180 {
181     type_register_static(&xen_evtchn_info);
182 }
183 
184 type_init(xen_evtchn_register_types)
185 
186 int xen_evtchn_set_callback_param(uint64_t param)
187 {
188     XenEvtchnState *s = xen_evtchn_singleton;
189     struct kvm_xen_hvm_attr xa = {
190         .type = KVM_XEN_ATTR_TYPE_UPCALL_VECTOR,
191         .u.vector = 0,
192     };
193     bool in_kernel = false;
194     int ret;
195 
196     if (!s) {
197         return -ENOTSUP;
198     }
199 
200     qemu_mutex_lock(&s->port_lock);
201 
202     switch (param >> CALLBACK_VIA_TYPE_SHIFT) {
203     case HVM_PARAM_CALLBACK_TYPE_VECTOR: {
204         xa.u.vector = (uint8_t)param,
205 
206         ret = kvm_vm_ioctl(kvm_state, KVM_XEN_HVM_SET_ATTR, &xa);
207         if (!ret && kvm_xen_has_cap(EVTCHN_SEND)) {
208             in_kernel = true;
209         }
210         break;
211     }
212     default:
213         /* Xen doesn't return error even if you set something bogus */
214         ret = 0;
215         break;
216     }
217 
218     if (!ret) {
219         /* If vector delivery was turned *off* then tell the kernel */
220         if ((s->callback_param >> CALLBACK_VIA_TYPE_SHIFT) ==
221             HVM_PARAM_CALLBACK_TYPE_VECTOR && !xa.u.vector) {
222             kvm_vm_ioctl(kvm_state, KVM_XEN_HVM_SET_ATTR, &xa);
223         }
224         s->callback_param = param;
225         s->evtchn_in_kernel = in_kernel;
226     }
227 
228     qemu_mutex_unlock(&s->port_lock);
229 
230     return ret;
231 }
232 
233 static void inject_callback(XenEvtchnState *s, uint32_t vcpu)
234 {
235     int type = s->callback_param >> CALLBACK_VIA_TYPE_SHIFT;
236 
237     kvm_xen_inject_vcpu_callback_vector(vcpu, type);
238 }
239 
240 static void deassign_kernel_port(evtchn_port_t port)
241 {
242     struct kvm_xen_hvm_attr ha;
243     int ret;
244 
245     ha.type = KVM_XEN_ATTR_TYPE_EVTCHN;
246     ha.u.evtchn.send_port = port;
247     ha.u.evtchn.flags = KVM_XEN_EVTCHN_DEASSIGN;
248 
249     ret = kvm_vm_ioctl(kvm_state, KVM_XEN_HVM_SET_ATTR, &ha);
250     if (ret) {
251         qemu_log_mask(LOG_GUEST_ERROR, "Failed to unbind kernel port %d: %s\n",
252                       port, strerror(ret));
253     }
254 }
255 
256 static int assign_kernel_port(uint16_t type, evtchn_port_t port,
257                               uint32_t vcpu_id)
258 {
259     CPUState *cpu = qemu_get_cpu(vcpu_id);
260     struct kvm_xen_hvm_attr ha;
261 
262     if (!cpu) {
263         return -ENOENT;
264     }
265 
266     ha.type = KVM_XEN_ATTR_TYPE_EVTCHN;
267     ha.u.evtchn.send_port = port;
268     ha.u.evtchn.type = type;
269     ha.u.evtchn.flags = 0;
270     ha.u.evtchn.deliver.port.port = port;
271     ha.u.evtchn.deliver.port.vcpu = kvm_arch_vcpu_id(cpu);
272     ha.u.evtchn.deliver.port.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL;
273 
274     return kvm_vm_ioctl(kvm_state, KVM_XEN_HVM_SET_ATTR, &ha);
275 }
276 
277 static bool valid_port(evtchn_port_t port)
278 {
279     if (!port) {
280         return false;
281     }
282 
283     if (xen_is_long_mode()) {
284         return port < EVTCHN_2L_NR_CHANNELS;
285     } else {
286         return port < COMPAT_EVTCHN_2L_NR_CHANNELS;
287     }
288 }
289 
290 static bool valid_vcpu(uint32_t vcpu)
291 {
292     return !!qemu_get_cpu(vcpu);
293 }
294 
295 int xen_evtchn_status_op(struct evtchn_status *status)
296 {
297     XenEvtchnState *s = xen_evtchn_singleton;
298     XenEvtchnPort *p;
299 
300     if (!s) {
301         return -ENOTSUP;
302     }
303 
304     if (status->dom != DOMID_SELF && status->dom != xen_domid) {
305         return -ESRCH;
306     }
307 
308     if (!valid_port(status->port)) {
309         return -EINVAL;
310     }
311 
312     qemu_mutex_lock(&s->port_lock);
313 
314     p = &s->port_table[status->port];
315 
316     status->status = p->type;
317     status->vcpu = p->vcpu;
318 
319     switch (p->type) {
320     case EVTCHNSTAT_unbound:
321         if (p->type_val & PORT_INFO_TYPEVAL_REMOTE_QEMU) {
322             status->u.unbound.dom = DOMID_QEMU;
323         } else {
324             status->u.unbound.dom = xen_domid;
325         }
326         break;
327 
328     case EVTCHNSTAT_interdomain:
329         if (p->type_val & PORT_INFO_TYPEVAL_REMOTE_QEMU) {
330             status->u.interdomain.dom = DOMID_QEMU;
331         } else {
332             status->u.interdomain.dom = xen_domid;
333         }
334 
335         status->u.interdomain.port = p->type_val &
336             PORT_INFO_TYPEVAL_REMOTE_PORT_MASK;
337         break;
338 
339     case EVTCHNSTAT_pirq:
340         status->u.pirq = p->type_val;
341         break;
342 
343     case EVTCHNSTAT_virq:
344         status->u.virq = p->type_val;
345         break;
346     }
347 
348     qemu_mutex_unlock(&s->port_lock);
349     return 0;
350 }
351 
352 /*
353  * Never thought I'd hear myself say this, but C++ templates would be
354  * kind of nice here.
355  *
356  * template<class T> static int do_unmask_port(T *shinfo, ...);
357  */
358 static int do_unmask_port_lm(XenEvtchnState *s, evtchn_port_t port,
359                              bool do_unmask, struct shared_info *shinfo,
360                              struct vcpu_info *vcpu_info)
361 {
362     const int bits_per_word = BITS_PER_BYTE * sizeof(shinfo->evtchn_pending[0]);
363     typeof(shinfo->evtchn_pending[0]) mask;
364     int idx = port / bits_per_word;
365     int offset = port % bits_per_word;
366 
367     mask = 1UL << offset;
368 
369     if (idx >= bits_per_word) {
370         return -EINVAL;
371     }
372 
373     if (do_unmask) {
374         /*
375          * If this is a true unmask operation, clear the mask bit. If
376          * it was already unmasked, we have nothing further to do.
377          */
378         if (!((qatomic_fetch_and(&shinfo->evtchn_mask[idx], ~mask) & mask))) {
379             return 0;
380         }
381     } else {
382         /*
383          * This is a pseudo-unmask for affinity changes. We don't
384          * change the mask bit, and if it's *masked* we have nothing
385          * else to do.
386          */
387         if (qatomic_fetch_or(&shinfo->evtchn_mask[idx], 0) & mask) {
388             return 0;
389         }
390     }
391 
392     /* If the event was not pending, we're done. */
393     if (!(qatomic_fetch_or(&shinfo->evtchn_pending[idx], 0) & mask)) {
394         return 0;
395     }
396 
397     /* Now on to the vcpu_info evtchn_pending_sel index... */
398     mask = 1UL << idx;
399 
400     /* If a port in this word was already pending for this vCPU, all done. */
401     if (qatomic_fetch_or(&vcpu_info->evtchn_pending_sel, mask) & mask) {
402         return 0;
403     }
404 
405     /* Set evtchn_upcall_pending for this vCPU */
406     if (qatomic_fetch_or(&vcpu_info->evtchn_upcall_pending, 1)) {
407         return 0;
408     }
409 
410     inject_callback(s, s->port_table[port].vcpu);
411 
412     return 0;
413 }
414 
415 static int do_unmask_port_compat(XenEvtchnState *s, evtchn_port_t port,
416                                  bool do_unmask,
417                                  struct compat_shared_info *shinfo,
418                                  struct compat_vcpu_info *vcpu_info)
419 {
420     const int bits_per_word = BITS_PER_BYTE * sizeof(shinfo->evtchn_pending[0]);
421     typeof(shinfo->evtchn_pending[0]) mask;
422     int idx = port / bits_per_word;
423     int offset = port % bits_per_word;
424 
425     mask = 1UL << offset;
426 
427     if (idx >= bits_per_word) {
428         return -EINVAL;
429     }
430 
431     if (do_unmask) {
432         /*
433          * If this is a true unmask operation, clear the mask bit. If
434          * it was already unmasked, we have nothing further to do.
435          */
436         if (!((qatomic_fetch_and(&shinfo->evtchn_mask[idx], ~mask) & mask))) {
437             return 0;
438         }
439     } else {
440         /*
441          * This is a pseudo-unmask for affinity changes. We don't
442          * change the mask bit, and if it's *masked* we have nothing
443          * else to do.
444          */
445         if (qatomic_fetch_or(&shinfo->evtchn_mask[idx], 0) & mask) {
446             return 0;
447         }
448     }
449 
450     /* If the event was not pending, we're done. */
451     if (!(qatomic_fetch_or(&shinfo->evtchn_pending[idx], 0) & mask)) {
452         return 0;
453     }
454 
455     /* Now on to the vcpu_info evtchn_pending_sel index... */
456     mask = 1UL << idx;
457 
458     /* If a port in this word was already pending for this vCPU, all done. */
459     if (qatomic_fetch_or(&vcpu_info->evtchn_pending_sel, mask) & mask) {
460         return 0;
461     }
462 
463     /* Set evtchn_upcall_pending for this vCPU */
464     if (qatomic_fetch_or(&vcpu_info->evtchn_upcall_pending, 1)) {
465         return 0;
466     }
467 
468     inject_callback(s, s->port_table[port].vcpu);
469 
470     return 0;
471 }
472 
473 static int unmask_port(XenEvtchnState *s, evtchn_port_t port, bool do_unmask)
474 {
475     void *vcpu_info, *shinfo;
476 
477     if (s->port_table[port].type == EVTCHNSTAT_closed) {
478         return -EINVAL;
479     }
480 
481     shinfo = xen_overlay_get_shinfo_ptr();
482     if (!shinfo) {
483         return -ENOTSUP;
484     }
485 
486     vcpu_info = kvm_xen_get_vcpu_info_hva(s->port_table[port].vcpu);
487     if (!vcpu_info) {
488         return -EINVAL;
489     }
490 
491     if (xen_is_long_mode()) {
492         return do_unmask_port_lm(s, port, do_unmask, shinfo, vcpu_info);
493     } else {
494         return do_unmask_port_compat(s, port, do_unmask, shinfo, vcpu_info);
495     }
496 }
497 
498 static int do_set_port_lm(XenEvtchnState *s, evtchn_port_t port,
499                           struct shared_info *shinfo,
500                           struct vcpu_info *vcpu_info)
501 {
502     const int bits_per_word = BITS_PER_BYTE * sizeof(shinfo->evtchn_pending[0]);
503     typeof(shinfo->evtchn_pending[0]) mask;
504     int idx = port / bits_per_word;
505     int offset = port % bits_per_word;
506 
507     mask = 1UL << offset;
508 
509     if (idx >= bits_per_word) {
510         return -EINVAL;
511     }
512 
513     /* Update the pending bit itself. If it was already set, we're done. */
514     if (qatomic_fetch_or(&shinfo->evtchn_pending[idx], mask) & mask) {
515         return 0;
516     }
517 
518     /* Check if it's masked. */
519     if (qatomic_fetch_or(&shinfo->evtchn_mask[idx], 0) & mask) {
520         return 0;
521     }
522 
523     /* Now on to the vcpu_info evtchn_pending_sel index... */
524     mask = 1UL << idx;
525 
526     /* If a port in this word was already pending for this vCPU, all done. */
527     if (qatomic_fetch_or(&vcpu_info->evtchn_pending_sel, mask) & mask) {
528         return 0;
529     }
530 
531     /* Set evtchn_upcall_pending for this vCPU */
532     if (qatomic_fetch_or(&vcpu_info->evtchn_upcall_pending, 1)) {
533         return 0;
534     }
535 
536     inject_callback(s, s->port_table[port].vcpu);
537 
538     return 0;
539 }
540 
541 static int do_set_port_compat(XenEvtchnState *s, evtchn_port_t port,
542                               struct compat_shared_info *shinfo,
543                               struct compat_vcpu_info *vcpu_info)
544 {
545     const int bits_per_word = BITS_PER_BYTE * sizeof(shinfo->evtchn_pending[0]);
546     typeof(shinfo->evtchn_pending[0]) mask;
547     int idx = port / bits_per_word;
548     int offset = port % bits_per_word;
549 
550     mask = 1UL << offset;
551 
552     if (idx >= bits_per_word) {
553         return -EINVAL;
554     }
555 
556     /* Update the pending bit itself. If it was already set, we're done. */
557     if (qatomic_fetch_or(&shinfo->evtchn_pending[idx], mask) & mask) {
558         return 0;
559     }
560 
561     /* Check if it's masked. */
562     if (qatomic_fetch_or(&shinfo->evtchn_mask[idx], 0) & mask) {
563         return 0;
564     }
565 
566     /* Now on to the vcpu_info evtchn_pending_sel index... */
567     mask = 1UL << idx;
568 
569     /* If a port in this word was already pending for this vCPU, all done. */
570     if (qatomic_fetch_or(&vcpu_info->evtchn_pending_sel, mask) & mask) {
571         return 0;
572     }
573 
574     /* Set evtchn_upcall_pending for this vCPU */
575     if (qatomic_fetch_or(&vcpu_info->evtchn_upcall_pending, 1)) {
576         return 0;
577     }
578 
579     inject_callback(s, s->port_table[port].vcpu);
580 
581     return 0;
582 }
583 
584 static int set_port_pending(XenEvtchnState *s, evtchn_port_t port)
585 {
586     void *vcpu_info, *shinfo;
587 
588     if (s->port_table[port].type == EVTCHNSTAT_closed) {
589         return -EINVAL;
590     }
591 
592     if (s->evtchn_in_kernel) {
593         XenEvtchnPort *p = &s->port_table[port];
594         CPUState *cpu = qemu_get_cpu(p->vcpu);
595         struct kvm_irq_routing_xen_evtchn evt;
596 
597         if (!cpu) {
598             return 0;
599         }
600 
601         evt.port = port;
602         evt.vcpu = kvm_arch_vcpu_id(cpu);
603         evt.priority = KVM_IRQ_ROUTING_XEN_EVTCHN_PRIO_2LEVEL;
604 
605         return kvm_vm_ioctl(kvm_state, KVM_XEN_HVM_EVTCHN_SEND, &evt);
606     }
607 
608     shinfo = xen_overlay_get_shinfo_ptr();
609     if (!shinfo) {
610         return -ENOTSUP;
611     }
612 
613     vcpu_info = kvm_xen_get_vcpu_info_hva(s->port_table[port].vcpu);
614     if (!vcpu_info) {
615         return -EINVAL;
616     }
617 
618     if (xen_is_long_mode()) {
619         return do_set_port_lm(s, port, shinfo, vcpu_info);
620     } else {
621         return do_set_port_compat(s, port, shinfo, vcpu_info);
622     }
623 }
624 
625 static int clear_port_pending(XenEvtchnState *s, evtchn_port_t port)
626 {
627     void *p = xen_overlay_get_shinfo_ptr();
628 
629     if (!p) {
630         return -ENOTSUP;
631     }
632 
633     if (xen_is_long_mode()) {
634         struct shared_info *shinfo = p;
635         const int bits_per_word = BITS_PER_BYTE * sizeof(shinfo->evtchn_pending[0]);
636         typeof(shinfo->evtchn_pending[0]) mask;
637         int idx = port / bits_per_word;
638         int offset = port % bits_per_word;
639 
640         mask = 1UL << offset;
641 
642         qatomic_fetch_and(&shinfo->evtchn_pending[idx], ~mask);
643     } else {
644         struct compat_shared_info *shinfo = p;
645         const int bits_per_word = BITS_PER_BYTE * sizeof(shinfo->evtchn_pending[0]);
646         typeof(shinfo->evtchn_pending[0]) mask;
647         int idx = port / bits_per_word;
648         int offset = port % bits_per_word;
649 
650         mask = 1UL << offset;
651 
652         qatomic_fetch_and(&shinfo->evtchn_pending[idx], ~mask);
653     }
654     return 0;
655 }
656 
657 static void free_port(XenEvtchnState *s, evtchn_port_t port)
658 {
659     s->port_table[port].type = EVTCHNSTAT_closed;
660     s->port_table[port].type_val = 0;
661     s->port_table[port].vcpu = 0;
662 
663     if (s->nr_ports == port + 1) {
664         do {
665             s->nr_ports--;
666         } while (s->nr_ports &&
667                  s->port_table[s->nr_ports - 1].type == EVTCHNSTAT_closed);
668     }
669 
670     /* Clear pending event to avoid unexpected behavior on re-bind. */
671     clear_port_pending(s, port);
672 }
673 
674 static int allocate_port(XenEvtchnState *s, uint32_t vcpu, uint16_t type,
675                          uint16_t val, evtchn_port_t *port)
676 {
677     evtchn_port_t p = 1;
678 
679     for (p = 1; valid_port(p); p++) {
680         if (s->port_table[p].type == EVTCHNSTAT_closed) {
681             s->port_table[p].vcpu = vcpu;
682             s->port_table[p].type = type;
683             s->port_table[p].type_val = val;
684 
685             *port = p;
686 
687             if (s->nr_ports < p + 1) {
688                 s->nr_ports = p + 1;
689             }
690 
691             return 0;
692         }
693     }
694     return -ENOSPC;
695 }
696 
697 static bool virq_is_global(uint32_t virq)
698 {
699     switch (virq) {
700     case VIRQ_TIMER:
701     case VIRQ_DEBUG:
702     case VIRQ_XENOPROF:
703     case VIRQ_XENPMU:
704         return false;
705 
706     default:
707         return true;
708     }
709 }
710 
711 static int close_port(XenEvtchnState *s, evtchn_port_t port)
712 {
713     XenEvtchnPort *p = &s->port_table[port];
714 
715     switch (p->type) {
716     case EVTCHNSTAT_closed:
717         return -ENOENT;
718 
719     case EVTCHNSTAT_virq:
720         kvm_xen_set_vcpu_virq(virq_is_global(p->type_val) ? 0 : p->vcpu,
721                               p->type_val, 0);
722         break;
723 
724     case EVTCHNSTAT_ipi:
725         if (s->evtchn_in_kernel) {
726             deassign_kernel_port(port);
727         }
728         break;
729 
730     case EVTCHNSTAT_interdomain:
731         if (p->type_val & PORT_INFO_TYPEVAL_REMOTE_QEMU) {
732             /* Not yet implemented. This can't happen! */
733         } else {
734             /* Loopback interdomain */
735             XenEvtchnPort *rp = &s->port_table[p->type_val];
736             if (!valid_port(p->type_val) || rp->type_val != port ||
737                 rp->type != EVTCHNSTAT_interdomain) {
738                 error_report("Inconsistent state for interdomain unbind");
739             } else {
740                 /* Set the other end back to unbound */
741                 rp->type = EVTCHNSTAT_unbound;
742                 rp->type_val = 0;
743             }
744         }
745         break;
746 
747     default:
748         break;
749     }
750 
751     free_port(s, port);
752     return 0;
753 }
754 
755 int xen_evtchn_soft_reset(void)
756 {
757     XenEvtchnState *s = xen_evtchn_singleton;
758     int i;
759 
760     if (!s) {
761         return -ENOTSUP;
762     }
763 
764     assert(qemu_mutex_iothread_locked());
765 
766     QEMU_LOCK_GUARD(&s->port_lock);
767 
768     for (i = 0; i < s->nr_ports; i++) {
769         close_port(s, i);
770     }
771 
772     return 0;
773 }
774 
775 int xen_evtchn_reset_op(struct evtchn_reset *reset)
776 {
777     if (reset->dom != DOMID_SELF && reset->dom != xen_domid) {
778         return -ESRCH;
779     }
780 
781     return xen_evtchn_soft_reset();
782 }
783 
784 int xen_evtchn_close_op(struct evtchn_close *close)
785 {
786     XenEvtchnState *s = xen_evtchn_singleton;
787     int ret;
788 
789     if (!s) {
790         return -ENOTSUP;
791     }
792 
793     if (!valid_port(close->port)) {
794         return -EINVAL;
795     }
796 
797     qemu_mutex_lock(&s->port_lock);
798 
799     ret = close_port(s, close->port);
800 
801     qemu_mutex_unlock(&s->port_lock);
802 
803     return ret;
804 }
805 
806 int xen_evtchn_unmask_op(struct evtchn_unmask *unmask)
807 {
808     XenEvtchnState *s = xen_evtchn_singleton;
809     int ret;
810 
811     if (!s) {
812         return -ENOTSUP;
813     }
814 
815     if (!valid_port(unmask->port)) {
816         return -EINVAL;
817     }
818 
819     qemu_mutex_lock(&s->port_lock);
820 
821     ret = unmask_port(s, unmask->port, true);
822 
823     qemu_mutex_unlock(&s->port_lock);
824 
825     return ret;
826 }
827 
828 int xen_evtchn_bind_vcpu_op(struct evtchn_bind_vcpu *vcpu)
829 {
830     XenEvtchnState *s = xen_evtchn_singleton;
831     XenEvtchnPort *p;
832     int ret = -EINVAL;
833 
834     if (!s) {
835         return -ENOTSUP;
836     }
837 
838     if (!valid_port(vcpu->port)) {
839         return -EINVAL;
840     }
841 
842     if (!valid_vcpu(vcpu->vcpu)) {
843         return -ENOENT;
844     }
845 
846     qemu_mutex_lock(&s->port_lock);
847 
848     p = &s->port_table[vcpu->port];
849 
850     if (p->type == EVTCHNSTAT_interdomain ||
851         p->type == EVTCHNSTAT_unbound ||
852         p->type == EVTCHNSTAT_pirq ||
853         (p->type == EVTCHNSTAT_virq && virq_is_global(p->type_val))) {
854         /*
855          * unmask_port() with do_unmask==false will just raise the event
856          * on the new vCPU if the port was already pending.
857          */
858         p->vcpu = vcpu->vcpu;
859         unmask_port(s, vcpu->port, false);
860         ret = 0;
861     }
862 
863     qemu_mutex_unlock(&s->port_lock);
864 
865     return ret;
866 }
867 
868 int xen_evtchn_bind_virq_op(struct evtchn_bind_virq *virq)
869 {
870     XenEvtchnState *s = xen_evtchn_singleton;
871     int ret;
872 
873     if (!s) {
874         return -ENOTSUP;
875     }
876 
877     if (virq->virq >= NR_VIRQS) {
878         return -EINVAL;
879     }
880 
881     /* Global VIRQ must be allocated on vCPU0 first */
882     if (virq_is_global(virq->virq) && virq->vcpu != 0) {
883         return -EINVAL;
884     }
885 
886     if (!valid_vcpu(virq->vcpu)) {
887         return -ENOENT;
888     }
889 
890     qemu_mutex_lock(&s->port_lock);
891 
892     ret = allocate_port(s, virq->vcpu, EVTCHNSTAT_virq, virq->virq,
893                         &virq->port);
894     if (!ret) {
895         ret = kvm_xen_set_vcpu_virq(virq->vcpu, virq->virq, virq->port);
896         if (ret) {
897             free_port(s, virq->port);
898         }
899     }
900 
901     qemu_mutex_unlock(&s->port_lock);
902 
903     return ret;
904 }
905 
906 int xen_evtchn_bind_ipi_op(struct evtchn_bind_ipi *ipi)
907 {
908     XenEvtchnState *s = xen_evtchn_singleton;
909     int ret;
910 
911     if (!s) {
912         return -ENOTSUP;
913     }
914 
915     if (!valid_vcpu(ipi->vcpu)) {
916         return -ENOENT;
917     }
918 
919     qemu_mutex_lock(&s->port_lock);
920 
921     ret = allocate_port(s, ipi->vcpu, EVTCHNSTAT_ipi, 0, &ipi->port);
922     if (!ret && s->evtchn_in_kernel) {
923         assign_kernel_port(EVTCHNSTAT_ipi, ipi->port, ipi->vcpu);
924     }
925 
926     qemu_mutex_unlock(&s->port_lock);
927 
928     return ret;
929 }
930 
931 int xen_evtchn_bind_interdomain_op(struct evtchn_bind_interdomain *interdomain)
932 {
933     XenEvtchnState *s = xen_evtchn_singleton;
934     uint16_t type_val;
935     int ret;
936 
937     if (!s) {
938         return -ENOTSUP;
939     }
940 
941     if (interdomain->remote_dom == DOMID_QEMU) {
942         type_val = PORT_INFO_TYPEVAL_REMOTE_QEMU;
943     } else if (interdomain->remote_dom == DOMID_SELF ||
944                interdomain->remote_dom == xen_domid) {
945         type_val = 0;
946     } else {
947         return -ESRCH;
948     }
949 
950     if (!valid_port(interdomain->remote_port)) {
951         return -EINVAL;
952     }
953 
954     qemu_mutex_lock(&s->port_lock);
955 
956     /* The newly allocated port starts out as unbound */
957     ret = allocate_port(s, 0, EVTCHNSTAT_unbound, type_val,
958                         &interdomain->local_port);
959     if (ret) {
960         goto out;
961     }
962 
963     if (interdomain->remote_dom == DOMID_QEMU) {
964         /* We haven't hooked up QEMU's PV drivers to this yet */
965         ret = -ENOSYS;
966     } else {
967         /* Loopback */
968         XenEvtchnPort *rp = &s->port_table[interdomain->remote_port];
969         XenEvtchnPort *lp = &s->port_table[interdomain->local_port];
970 
971         if (rp->type == EVTCHNSTAT_unbound && rp->type_val == 0) {
972             /* It's a match! */
973             rp->type = EVTCHNSTAT_interdomain;
974             rp->type_val = interdomain->local_port;
975 
976             lp->type = EVTCHNSTAT_interdomain;
977             lp->type_val = interdomain->remote_port;
978         } else {
979             ret = -EINVAL;
980         }
981     }
982 
983     if (ret) {
984         free_port(s, interdomain->local_port);
985     }
986  out:
987     qemu_mutex_unlock(&s->port_lock);
988 
989     return ret;
990 
991 }
992 int xen_evtchn_alloc_unbound_op(struct evtchn_alloc_unbound *alloc)
993 {
994     XenEvtchnState *s = xen_evtchn_singleton;
995     uint16_t type_val;
996     int ret;
997 
998     if (!s) {
999         return -ENOTSUP;
1000     }
1001 
1002     if (alloc->dom != DOMID_SELF && alloc->dom != xen_domid) {
1003         return -ESRCH;
1004     }
1005 
1006     if (alloc->remote_dom == DOMID_QEMU) {
1007         type_val = PORT_INFO_TYPEVAL_REMOTE_QEMU;
1008     } else if (alloc->remote_dom == DOMID_SELF ||
1009                alloc->remote_dom == xen_domid) {
1010         type_val = 0;
1011     } else {
1012         return -EPERM;
1013     }
1014 
1015     qemu_mutex_lock(&s->port_lock);
1016 
1017     ret = allocate_port(s, 0, EVTCHNSTAT_unbound, type_val, &alloc->port);
1018 
1019     qemu_mutex_unlock(&s->port_lock);
1020 
1021     return ret;
1022 }
1023 
1024 int xen_evtchn_send_op(struct evtchn_send *send)
1025 {
1026     XenEvtchnState *s = xen_evtchn_singleton;
1027     XenEvtchnPort *p;
1028     int ret = 0;
1029 
1030     if (!s) {
1031         return -ENOTSUP;
1032     }
1033 
1034     if (!valid_port(send->port)) {
1035         return -EINVAL;
1036     }
1037 
1038     qemu_mutex_lock(&s->port_lock);
1039 
1040     p = &s->port_table[send->port];
1041 
1042     switch (p->type) {
1043     case EVTCHNSTAT_interdomain:
1044         if (p->type_val & PORT_INFO_TYPEVAL_REMOTE_QEMU) {
1045             /*
1046              * This is an event from the guest to qemu itself, which is
1047              * serving as the driver domain. Not yet implemented; it will
1048              * be hooked up to the qemu implementation of xenstore,
1049              * console, PV net/block drivers etc.
1050              */
1051             ret = -ENOSYS;
1052         } else {
1053             /* Loopback interdomain ports; just a complex IPI */
1054             set_port_pending(s, p->type_val);
1055         }
1056         break;
1057 
1058     case EVTCHNSTAT_ipi:
1059         set_port_pending(s, send->port);
1060         break;
1061 
1062     case EVTCHNSTAT_unbound:
1063         /* Xen will silently drop these */
1064         break;
1065 
1066     default:
1067         ret = -EINVAL;
1068         break;
1069     }
1070 
1071     qemu_mutex_unlock(&s->port_lock);
1072 
1073     return ret;
1074 }
1075 
1076 EvtchnInfoList *qmp_xen_event_list(Error **errp)
1077 {
1078     XenEvtchnState *s = xen_evtchn_singleton;
1079     EvtchnInfoList *head = NULL, **tail = &head;
1080     void *shinfo, *pending, *mask;
1081     int i;
1082 
1083     if (!s) {
1084         error_setg(errp, "Xen event channel emulation not enabled");
1085         return NULL;
1086     }
1087 
1088     shinfo = xen_overlay_get_shinfo_ptr();
1089     if (!shinfo) {
1090         error_setg(errp, "Xen shared info page not allocated");
1091         return NULL;
1092     }
1093 
1094     if (xen_is_long_mode()) {
1095         pending = shinfo + offsetof(struct shared_info, evtchn_pending);
1096         mask = shinfo + offsetof(struct shared_info, evtchn_mask);
1097     } else {
1098         pending = shinfo + offsetof(struct compat_shared_info, evtchn_pending);
1099         mask = shinfo + offsetof(struct compat_shared_info, evtchn_mask);
1100     }
1101 
1102     QEMU_LOCK_GUARD(&s->port_lock);
1103 
1104     for (i = 0; i < s->nr_ports; i++) {
1105         XenEvtchnPort *p = &s->port_table[i];
1106         EvtchnInfo *info;
1107 
1108         if (p->type == EVTCHNSTAT_closed) {
1109             continue;
1110         }
1111 
1112         info = g_new0(EvtchnInfo, 1);
1113 
1114         info->port = i;
1115         qemu_build_assert(EVTCHN_PORT_TYPE_CLOSED == EVTCHNSTAT_closed);
1116         qemu_build_assert(EVTCHN_PORT_TYPE_UNBOUND == EVTCHNSTAT_unbound);
1117         qemu_build_assert(EVTCHN_PORT_TYPE_INTERDOMAIN == EVTCHNSTAT_interdomain);
1118         qemu_build_assert(EVTCHN_PORT_TYPE_PIRQ == EVTCHNSTAT_pirq);
1119         qemu_build_assert(EVTCHN_PORT_TYPE_VIRQ == EVTCHNSTAT_virq);
1120         qemu_build_assert(EVTCHN_PORT_TYPE_IPI == EVTCHNSTAT_ipi);
1121 
1122         info->type = p->type;
1123         if (p->type == EVTCHNSTAT_interdomain) {
1124             info->remote_domain = g_strdup((p->type_val & PORT_INFO_TYPEVAL_REMOTE_QEMU) ?
1125                                            "qemu" : "loopback");
1126             info->target = p->type_val & PORT_INFO_TYPEVAL_REMOTE_PORT_MASK;
1127         } else {
1128             info->target = p->type_val;
1129         }
1130         info->vcpu = p->vcpu;
1131         info->pending = test_bit(i, pending);
1132         info->masked = test_bit(i, mask);
1133 
1134         QAPI_LIST_APPEND(tail, info);
1135     }
1136 
1137     return head;
1138 }
1139 
1140 void qmp_xen_event_inject(uint32_t port, Error **errp)
1141 {
1142     XenEvtchnState *s = xen_evtchn_singleton;
1143 
1144     if (!s) {
1145         error_setg(errp, "Xen event channel emulation not enabled");
1146         return;
1147     }
1148 
1149     if (!valid_port(port)) {
1150         error_setg(errp, "Invalid port %u", port);
1151     }
1152 
1153     QEMU_LOCK_GUARD(&s->port_lock);
1154 
1155     if (set_port_pending(s, port)) {
1156         error_setg(errp, "Failed to set port %u", port);
1157         return;
1158     }
1159 }
1160 
1161 void hmp_xen_event_list(Monitor *mon, const QDict *qdict)
1162 {
1163     EvtchnInfoList *iter, *info_list;
1164     Error *err = NULL;
1165 
1166     info_list = qmp_xen_event_list(&err);
1167     if (err) {
1168         hmp_handle_error(mon, err);
1169         return;
1170     }
1171 
1172     for (iter = info_list; iter; iter = iter->next) {
1173         EvtchnInfo *info = iter->value;
1174 
1175         monitor_printf(mon, "port %4u: vcpu: %d %s", info->port, info->vcpu,
1176                        EvtchnPortType_str(info->type));
1177         if (info->type != EVTCHN_PORT_TYPE_IPI) {
1178             monitor_printf(mon,  "(");
1179             if (info->remote_domain) {
1180                 monitor_printf(mon, "%s:", info->remote_domain);
1181             }
1182             monitor_printf(mon, "%d)", info->target);
1183         }
1184         if (info->pending) {
1185             monitor_printf(mon, " PENDING");
1186         }
1187         if (info->masked) {
1188             monitor_printf(mon, " MASKED");
1189         }
1190         monitor_printf(mon, "\n");
1191     }
1192 
1193     qapi_free_EvtchnInfoList(info_list);
1194 }
1195 
1196 void hmp_xen_event_inject(Monitor *mon, const QDict *qdict)
1197 {
1198     int port = qdict_get_int(qdict, "port");
1199     Error *err = NULL;
1200 
1201     qmp_xen_event_inject(port, &err);
1202     if (err) {
1203         hmp_handle_error(mon, err);
1204     } else {
1205         monitor_printf(mon, "Delivered port %d\n", port);
1206     }
1207 }
1208 
1209