*
* Copyright (c) 2017 Citrix Systems Ltd.
*/
+#include <xen/event.h>
+#include <xen/guest_access.h>
#include <xen/hypercall.h>
#include <xen/init.h>
#include <xen/shutdown.h>
boolean_param("pv-shim", pv_shim);
#endif
+static struct domain *guest;
+
+static long pv_shim_event_channel_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg);
+
#define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_USER| \
_PAGE_GUEST_KERNEL)
#define COMPAT_L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED)
: COMPAT_L1_PROT));
}
+static void evtchn_reserve(struct domain *d, unsigned int port)
+{
+ ASSERT(port_is_valid(d, port));
+ evtchn_from_port(d, port)->state = ECS_RESERVED;
+ BUG_ON(xen_hypercall_evtchn_unmask(port));
+}
+
+static bool evtchn_handled(struct domain *d, unsigned int port)
+{
+ ASSERT(port_is_valid(d, port));
+ /* The shim manages VIRQs, the rest is forwarded to L0. */
+ return evtchn_from_port(d, port)->state == ECS_VIRQ;
+}
+
+static void evtchn_assign_vcpu(struct domain *d, unsigned int port,
+ unsigned int vcpu)
+{
+ ASSERT(port_is_valid(d, port));
+ evtchn_from_port(d, port)->notify_vcpu_id = vcpu;
+}
+
void __init pv_shim_setup_dom(struct domain *d, l4_pgentry_t *l4start,
unsigned long va_start, unsigned long store_va,
unsigned long console_va, unsigned long vphysmap,
replace_va_mapping(d, l4start, va, param); \
dom0_update_physmap(d, PFN_DOWN((va) - va_start), param, vphysmap); \
} \
+ else \
+ { \
+ BUG_ON(evtchn_allocate_port(d, param)); \
+ evtchn_reserve(d, param); \
+ } \
})
SET_AND_MAP_PARAM(HVM_PARAM_STORE_PFN, si->store_mfn, store_va);
SET_AND_MAP_PARAM(HVM_PARAM_STORE_EVTCHN, si->store_evtchn, 0);
SET_AND_MAP_PARAM(HVM_PARAM_CONSOLE_EVTCHN, si->console.domU.evtchn, 0);
}
#undef SET_AND_MAP_PARAM
+ pv_hypercall_table_replace(__HYPERVISOR_event_channel_op,
+ (hypercall_fn_t *)pv_shim_event_channel_op,
+ (hypercall_fn_t *)pv_shim_event_channel_op);
+ guest = d;
}
void pv_shim_shutdown(uint8_t reason)
xen_hypercall_shutdown(reason);
}
+static long pv_shim_event_channel_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
+{
+ struct domain *d = current->domain;
+ struct evtchn_close close;
+ long rc;
+
+ switch ( cmd )
+ {
+#define EVTCHN_FORWARD(cmd, port_field) \
+ case EVTCHNOP_##cmd: { \
+ struct evtchn_##cmd op; \
+ \
+ if ( copy_from_guest(&op, arg, 1) != 0 ) \
+ return -EFAULT; \
+ \
+ rc = xen_hypercall_event_channel_op(EVTCHNOP_##cmd, &op); \
+ if ( rc ) \
+ break; \
+ \
+ spin_lock(&d->event_lock); \
+ rc = evtchn_allocate_port(d, op.port_field); \
+ if ( rc ) \
+ { \
+ close.port = op.port_field; \
+ BUG_ON(xen_hypercall_event_channel_op(EVTCHNOP_close, &close)); \
+ } \
+ else \
+ evtchn_reserve(d, op.port_field); \
+ spin_unlock(&d->event_lock); \
+ \
+ if ( !rc && __copy_to_guest(arg, &op, 1) ) \
+ rc = -EFAULT; \
+ \
+ break; \
+ }
+
+ EVTCHN_FORWARD(alloc_unbound, port)
+ EVTCHN_FORWARD(bind_interdomain, local_port)
+#undef EVTCHN_FORWARD
+
+ case EVTCHNOP_bind_virq: {
+ struct evtchn_bind_virq virq;
+ struct evtchn_alloc_unbound alloc = {
+ .dom = DOMID_SELF,
+ .remote_dom = DOMID_SELF,
+ };
+
+ if ( copy_from_guest(&virq, arg, 1) != 0 )
+ return -EFAULT;
+ /*
+ * The event channel space is actually controlled by L0 Xen, so
+ * allocate a port from L0 and then force the VIRQ to be bound to that
+ * specific port.
+ *
+ * This is only required for VIRQ because the rest of the event channel
+ * operations are handled directly by L0.
+ */
+ rc = xen_hypercall_event_channel_op(EVTCHNOP_alloc_unbound, &alloc);
+ if ( rc )
+ break;
+
+ /* Force L1 to use the event channel port allocated on L0. */
+ rc = evtchn_bind_virq(&virq, alloc.port);
+ if ( rc )
+ {
+ close.port = alloc.port;
+ BUG_ON(xen_hypercall_event_channel_op(EVTCHNOP_close, &close));
+ }
+
+ if ( !rc && __copy_to_guest(arg, &virq, 1) )
+ rc = -EFAULT;
+
+ break;
+ }
+
+ case EVTCHNOP_status: {
+ struct evtchn_status status;
+
+ if ( copy_from_guest(&status, arg, 1) != 0 )
+ return -EFAULT;
+
+ /*
+ * NB: if the event channel is not handled by the shim, just forward
+ * the status request to L0, even if the port is not valid.
+ */
+ if ( port_is_valid(d, status.port) && evtchn_handled(d, status.port) )
+ rc = evtchn_status(&status);
+ else
+ rc = xen_hypercall_event_channel_op(EVTCHNOP_status, &status);
+
+ break;
+ }
+
+ case EVTCHNOP_bind_vcpu: {
+ struct evtchn_bind_vcpu vcpu;
+
+ if ( copy_from_guest(&vcpu, arg, 1) != 0 )
+ return -EFAULT;
+
+ if ( !port_is_valid(d, vcpu.port) )
+ return -EINVAL;
+
+ if ( evtchn_handled(d, vcpu.port) )
+ rc = evtchn_bind_vcpu(vcpu.port, vcpu.vcpu);
+ else
+ {
+ rc = xen_hypercall_event_channel_op(EVTCHNOP_bind_vcpu, &vcpu);
+ if ( !rc )
+ evtchn_assign_vcpu(d, vcpu.port, vcpu.vcpu);
+ }
+
+ break;
+ }
+
+ case EVTCHNOP_close: {
+ if ( copy_from_guest(&close, arg, 1) != 0 )
+ return -EFAULT;
+
+ if ( !port_is_valid(d, close.port) )
+ return -EINVAL;
+
+ set_bit(close.port, XEN_shared_info->evtchn_mask);
+
+ if ( evtchn_handled(d, close.port) )
+ {
+ rc = evtchn_close(d, close.port, true);
+ if ( rc )
+ break;
+ }
+ else
+ evtchn_free(d, evtchn_from_port(d, close.port));
+
+ rc = xen_hypercall_event_channel_op(EVTCHNOP_close, &close);
+ if ( rc )
+ /*
+ * If the port cannot be closed on the L0 mark it as reserved
+ * in the shim to avoid re-using it.
+ */
+ evtchn_reserve(d, close.port);
+
+ break;
+ }
+
+ case EVTCHNOP_bind_ipi: {
+ struct evtchn_bind_ipi ipi;
+
+ if ( copy_from_guest(&ipi, arg, 1) != 0 )
+ return -EFAULT;
+
+ rc = xen_hypercall_event_channel_op(EVTCHNOP_bind_ipi, &ipi);
+ if ( rc )
+ break;
+
+ spin_lock(&d->event_lock);
+ rc = evtchn_allocate_port(d, ipi.port);
+ if ( rc )
+ {
+ spin_unlock(&d->event_lock);
+
+ close.port = ipi.port;
+ BUG_ON(xen_hypercall_event_channel_op(EVTCHNOP_close, &close));
+ break;
+ }
+
+ evtchn_assign_vcpu(d, ipi.port, ipi.vcpu);
+ evtchn_reserve(d, ipi.port);
+ spin_unlock(&d->event_lock);
+
+ if ( __copy_to_guest(arg, &ipi, 1) )
+ rc = -EFAULT;
+
+ break;
+ }
+
+ case EVTCHNOP_unmask: {
+ struct evtchn_unmask unmask;
+
+ if ( copy_from_guest(&unmask, arg, 1) != 0 )
+ return -EFAULT;
+
+ /* Unmask is handled in L1 */
+ rc = evtchn_unmask(unmask.port);
+
+ break;
+ }
+
+ case EVTCHNOP_send: {
+ struct evtchn_send send;
+
+ if ( copy_from_guest(&send, arg, 1) != 0 )
+ return -EFAULT;
+
+ rc = xen_hypercall_event_channel_op(EVTCHNOP_send, &send);
+
+ break;
+ }
+
+ case EVTCHNOP_reset: {
+ struct evtchn_reset reset;
+
+ if ( copy_from_guest(&reset, arg, 1) != 0 )
+ return -EFAULT;
+
+ rc = xen_hypercall_event_channel_op(EVTCHNOP_reset, &reset);
+
+ break;
+ }
+
+ default:
+ /* No FIFO or PIRQ support for now */
+ rc = -EOPNOTSUPP;
+ break;
+ }
+
+ return rc;
+}
+
+void pv_shim_inject_evtchn(unsigned int port)
+{
+ if ( port_is_valid(guest, port) )
+ {
+ struct evtchn *chn = evtchn_from_port(guest, port);
+
+ evtchn_port_set_pending(guest, chn->notify_vcpu_id, chn);
+ }
+}
+
domid_t get_initial_domain_id(void)
{
uint32_t eax, ebx, ecx, edx;
xfree(bucket);
}
+int evtchn_allocate_port(struct domain *d, evtchn_port_t port)
+{
+ if ( port > d->max_evtchn_port || port >= d->max_evtchns )
+ return -ENOSPC;
+
+ if ( port_is_valid(d, port) )
+ {
+ if ( evtchn_from_port(d, port)->state != ECS_FREE ||
+ evtchn_port_is_busy(d, port) )
+ return -EBUSY;
+ }
+ else
+ {
+ struct evtchn *chn;
+ struct evtchn **grp;
+
+ if ( !group_from_port(d, port) )
+ {
+ grp = xzalloc_array(struct evtchn *, BUCKETS_PER_GROUP);
+ if ( !grp )
+ return -ENOMEM;
+ group_from_port(d, port) = grp;
+ }
+
+ chn = alloc_evtchn_bucket(d, port);
+ if ( !chn )
+ return -ENOMEM;
+ bucket_from_port(d, port) = chn;
+
+ write_atomic(&d->valid_evtchns, d->valid_evtchns + EVTCHNS_PER_BUCKET);
+ }
+
+ return 0;
+}
+
static int get_free_port(struct domain *d)
{
- struct evtchn *chn;
- struct evtchn **grp;
int port;
if ( d->is_dying )
return -EINVAL;
- for ( port = 0; port_is_valid(d, port); port++ )
+ for ( port = 0; port <= d->max_evtchn_port; port++ )
{
- if ( port > d->max_evtchn_port )
- return -ENOSPC;
- if ( evtchn_from_port(d, port)->state == ECS_FREE
- && !evtchn_port_is_busy(d, port) )
- return port;
- }
+ int rc = evtchn_allocate_port(d, port);
- if ( port == d->max_evtchns || port > d->max_evtchn_port )
- return -ENOSPC;
+ if ( rc == -EBUSY )
+ continue;
- if ( !group_from_port(d, port) )
- {
- grp = xzalloc_array(struct evtchn *, BUCKETS_PER_GROUP);
- if ( !grp )
- return -ENOMEM;
- group_from_port(d, port) = grp;
+ return port;
}
- chn = alloc_evtchn_bucket(d, port);
- if ( !chn )
- return -ENOMEM;
- bucket_from_port(d, port) = chn;
-
- write_atomic(&d->valid_evtchns, d->valid_evtchns + EVTCHNS_PER_BUCKET);
-
- return port;
+ return -ENOSPC;
}
-static void free_evtchn(struct domain *d, struct evtchn *chn)
+void evtchn_free(struct domain *d, struct evtchn *chn)
{
/* Clear pending event to avoid unexpected behavior on re-bind. */
evtchn_port_clear_pending(d, chn);
}
-static long evtchn_bind_virq(evtchn_bind_virq_t *bind)
+int evtchn_bind_virq(evtchn_bind_virq_t *bind, evtchn_port_t port)
{
struct evtchn *chn;
struct vcpu *v;
struct domain *d = current->domain;
- int port, virq = bind->virq, vcpu = bind->vcpu;
- long rc = 0;
+ int virq = bind->virq, vcpu = bind->vcpu;
+ int rc = 0;
if ( (virq < 0) || (virq >= ARRAY_SIZE(v->virq_to_evtchn)) )
return -EINVAL;
if ( v->virq_to_evtchn[virq] != 0 )
ERROR_EXIT(-EEXIST);
- if ( (port = get_free_port(d)) < 0 )
- ERROR_EXIT(port);
+ if ( port != 0 )
+ {
+ if ( (rc = evtchn_allocate_port(d, port)) != 0 )
+ ERROR_EXIT(rc);
+ }
+ else
+ {
+ int alloc_port = get_free_port(d);
+
+ if ( alloc_port < 0 )
+ ERROR_EXIT(alloc_port);
+ port = alloc_port;
+ }
chn = evtchn_from_port(d, port);
}
-static long evtchn_close(struct domain *d1, int port1, bool_t guest)
+int evtchn_close(struct domain *d1, int port1, bool guest)
{
struct domain *d2 = NULL;
struct vcpu *v;
double_evtchn_lock(chn1, chn2);
- free_evtchn(d1, chn1);
+ evtchn_free(d1, chn1);
chn2->state = ECS_UNBOUND;
chn2->u.unbound.remote_domid = d1->domain_id;
}
spin_lock(&chn1->lock);
- free_evtchn(d1, chn1);
+ evtchn_free(d1, chn1);
spin_unlock(&chn1->lock);
out:
}
}
-static long evtchn_status(evtchn_status_t *status)
+int evtchn_status(evtchn_status_t *status)
{
struct domain *d;
domid_t dom = status->dom;
struct evtchn_bind_virq bind_virq;
if ( copy_from_guest(&bind_virq, arg, 1) != 0 )
return -EFAULT;
- rc = evtchn_bind_virq(&bind_virq);
+ rc = evtchn_bind_virq(&bind_virq, 0);
if ( !rc && __copy_to_guest(arg, &bind_virq, 1) )
rc = -EFAULT; /* Cleaning up here would be a mess! */
break;