#include "private.h"
-static int all_restrict_cb(Xentoolcore__Active_Handle *ah, domid_t domid) {
+static int all_restrict_cb(Xentoolcore__Active_Handle *ah, domid_t domid)
+{
xenevtchn_handle *xce = CONTAINER_OF(ah, *xce, tc_ah);
- if (xce->fd < 0)
+ if ( xce->fd < 0 )
/* just in case */
return 0;
xenevtchn_handle *xce = malloc(sizeof(*xce));
int rc;
- if (!xce) return NULL;
+ if ( !xce )
+ return NULL;
xce->fd = -1;
xce->logger = logger;
xce->tc_ah.restrict_callback = all_restrict_cb;
xentoolcore__register_active_handle(&xce->tc_ah);
- if (!xce->logger) {
- xce->logger = xce->logger_tofree =
- (xentoollog_logger*)
+ if ( !xce->logger )
+ {
+ xce->logger = xce->logger_tofree = (xentoollog_logger *)
xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
- if (!xce->logger) goto err;
+ if ( !xce->logger )
+ goto err;
}
rc = osdep_evtchn_open(xce);
- if ( rc < 0 ) goto err;
+ if ( rc < 0 )
+ goto err;
return xce;
-err:
+ err:
xentoolcore__deregister_active_handle(&xce->tc_ah);
osdep_evtchn_close(xce);
xtl_logger_destroy(xce->logger_tofree);
free(xce);
+
return NULL;
}
rc = osdep_evtchn_close(xce);
xtl_logger_destroy(xce->logger_tofree);
free(xce);
+
return rc;
}
int osdep_evtchn_open(xenevtchn_handle *xce)
{
int fd = open(EVTCHN_DEV, O_RDWR|O_CLOEXEC);
+
if ( fd == -1 )
return -1;
+
xce->fd = fd;
+
return 0;
}
int osdep_evtchn_restrict(xenevtchn_handle *xce, domid_t domid)
{
errno = -EOPNOTSUPP;
+
return -1;
}
return ioctl(fd, IOCTL_EVTCHN_NOTIFY, ¬ify);
}
-xenevtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, uint32_t domid)
+xenevtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce,
+ uint32_t domid)
{
int ret, fd = xce->fd;
struct ioctl_evtchn_bind_unbound_port bind;
bind.remote_domain = domid;
ret = ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
- return ( ret == 0 ) ? bind.port : ret;
+
+ return ret ?: bind.port;
}
-xenevtchn_port_or_error_t
-xenevtchn_bind_interdomain(xenevtchn_handle *xce, uint32_t domid, evtchn_port_t remote_port)
+xenevtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce,
+ uint32_t domid,
+ evtchn_port_t remote_port)
{
int ret, fd = xce->fd;
struct ioctl_evtchn_bind_interdomain bind;
bind.remote_port = remote_port;
ret = ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
- return ( ret == 0 ) ? bind.port : ret;
+
+ return ret ?: bind.port;
}
-xenevtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce, unsigned int virq)
+xenevtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce,
+ unsigned int virq)
{
int ret, fd = xce->fd;
struct ioctl_evtchn_bind_virq bind;
bind.virq = virq;
ret = ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
- return ( ret == 0 ) ? bind.port : ret;
+
+ return ret ?: bind.port;
}
int xenevtchn_unbind(xenevtchn_handle *xce, evtchn_port_t port)
if ( write(fd, &port, sizeof(port)) != sizeof(port) )
return -1;
+
return 0;
}
int osdep_evtchn_open(xenevtchn_handle *xce)
{
int fd = open("/dev/xen/evtchn", O_RDWR|O_CLOEXEC);
+
if ( fd == -1 )
return -1;
+
xce->fd = fd;
+
return 0;
}
}
xenevtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce,
- uint32_t domid)
+ uint32_t domid)
{
int fd = xce->fd;
struct ioctl_evtchn_bind_unbound_port bind;
}
xenevtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce,
- uint32_t domid,
- evtchn_port_t remote_port)
+ uint32_t domid,
+ evtchn_port_t remote_port)
{
int fd = xce->fd;
struct ioctl_evtchn_bind_interdomain bind;
}
xenevtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce,
- unsigned int virq)
+ unsigned int virq)
{
int fd = xce->fd;
struct ioctl_evtchn_bind_virq bind;
if ( write(fd, &port, sizeof(port)) != sizeof(port) )
return -1;
+
return 0;
}
extern struct wait_queue_head event_queue;
/* XXX Note: This is not threadsafe */
-static struct evtchn_port_info* port_alloc(int fd) {
+static struct evtchn_port_info *port_alloc(int fd)
+{
struct evtchn_port_info *port_info;
+
port_info = malloc(sizeof(struct evtchn_port_info));
- if (port_info == NULL)
+ if ( port_info == NULL )
return NULL;
+
port_info->pending = 0;
port_info->port = -1;
port_info->bound = 0;
LIST_INSERT_HEAD(&files[fd].evtchn.ports, port_info, list);
+
return port_info;
}
-static void port_dealloc(struct evtchn_port_info *port_info) {
- if (port_info->bound)
+static void port_dealloc(struct evtchn_port_info *port_info)
+{
+ if ( port_info->bound )
unbind_evtchn(port_info->port);
+
LIST_REMOVE(port_info, list);
free(port_info);
}
int osdep_evtchn_open(xenevtchn_handle *xce)
{
int fd = alloc_fd(FTYPE_EVTCHN);
+
if ( fd == -1 )
return -1;
+
LIST_INIT(&files[fd].evtchn.ports);
xce->fd = fd;
printf("evtchn_open() -> %d\n", fd);
+
return 0;
}
int osdep_evtchn_restrict(xenevtchn_handle *xce, domid_t domid)
{
errno = -EOPNOTSUPP;
+
return -1;
}
void minios_evtchn_close_fd(int fd)
{
struct evtchn_port_info *port_info, *tmp;
+
LIST_FOREACH_SAFE(port_info, &files[fd].evtchn.ports, list, tmp)
port_dealloc(port_info);
ret = notify_remote_via_evtchn(port);
- if (ret < 0) {
+ if ( ret < 0 )
+ {
errno = -ret;
ret = -1;
}
+
return ret;
}
{
int fd = (int)(intptr_t)data;
struct evtchn_port_info *port_info;
+
assert(files[fd].type == FTYPE_EVTCHN);
mask_evtchn(port);
- LIST_FOREACH(port_info, &files[fd].evtchn.ports, list) {
- if (port_info->port == port)
+ LIST_FOREACH(port_info, &files[fd].evtchn.ports, list)
+ {
+ if ( port_info->port == port )
goto found;
}
+
printk("Unknown port for handle %d\n", fd);
return;
wake_up(&event_queue);
}
-xenevtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, uint32_t domid)
+xenevtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce,
+ uint32_t domid)
{
int fd = xce->fd;
struct evtchn_port_info *port_info;
assert(get_current() == main_thread);
port_info = port_alloc(fd);
- if (port_info == NULL)
+ if ( port_info == NULL )
return -1;
printf("xenevtchn_bind_unbound_port(%d)", domid);
- ret = evtchn_alloc_unbound(domid, evtchn_handler, (void*)(intptr_t)fd, &port);
+ ret = evtchn_alloc_unbound(domid, evtchn_handler,
+ (void *)(intptr_t)fd, &port);
printf(" = %d\n", ret);
- if (ret < 0) {
+ if ( ret < 0 )
+ {
port_dealloc(port_info);
errno = -ret;
return -1;
}
+
port_info->bound = 1;
port_info->port = port;
unmask_evtchn(port);
+
return port;
}
-xenevtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce, uint32_t domid,
- evtchn_port_t remote_port)
+xenevtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce,
+ uint32_t domid,
+ evtchn_port_t remote_port)
{
int fd = xce->fd;
struct evtchn_port_info *port_info;
assert(get_current() == main_thread);
port_info = port_alloc(fd);
- if (port_info == NULL)
+ if ( port_info == NULL )
return -1;
printf("xenevtchn_bind_interdomain(%d, %"PRId32")", domid, remote_port);
- ret = evtchn_bind_interdomain(domid, remote_port, evtchn_handler, (void*)(intptr_t)fd, &local_port);
+ ret = evtchn_bind_interdomain(domid, remote_port, evtchn_handler,
+ (void *)(intptr_t)fd, &local_port);
printf(" = %d\n", ret);
- if (ret < 0) {
+ if ( ret < 0 )
+ {
port_dealloc(port_info);
errno = -ret;
return -1;
}
+
port_info->bound = 1;
port_info->port = local_port;
unmask_evtchn(local_port);
+
return local_port;
}
int fd = xce->fd;
struct evtchn_port_info *port_info;
- LIST_FOREACH(port_info, &files[fd].evtchn.ports, list) {
- if (port_info->port == port) {
+ LIST_FOREACH(port_info, &files[fd].evtchn.ports, list)
+ {
+ if ( port_info->port == port )
+ {
port_dealloc(port_info);
return 0;
}
}
- printf("Warning: couldn't find port %"PRId32" for xc handle %x\n", port, fd);
+
+ printf("Warning: couldn't find port %"PRId32" for xc handle %x\n",
+ port, fd);
errno = EINVAL;
+
return -1;
}
-xenevtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce, unsigned int virq)
+xenevtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce,
+ unsigned int virq)
{
int fd = xce->fd;
struct evtchn_port_info *port_info;
assert(get_current() == main_thread);
port_info = port_alloc(fd);
- if (port_info == NULL)
+ if ( port_info == NULL )
return -1;
printf("xenevtchn_bind_virq(%d)", virq);
- port = bind_virq(virq, evtchn_handler, (void*)(intptr_t)fd);
+ port = bind_virq(virq, evtchn_handler, (void *)(intptr_t)fd);
printf(" = %d\n", port);
- if (port < 0) {
+ if ( port < 0 )
+ {
port_dealloc(port_info);
errno = -port;
return -1;
}
+
port_info->bound = 1;
port_info->port = port;
unmask_evtchn(port);
+
return port;
}
evtchn_port_t ret = -1;
local_irq_save(flags);
+
files[fd].read = 0;
- LIST_FOREACH(port_info, &files[fd].evtchn.ports, list) {
- if (port_info->port != -1 && port_info->pending) {
- if (ret == -1) {
+ LIST_FOREACH(port_info, &files[fd].evtchn.ports, list)
+ {
+ if ( port_info->port != -1 && port_info->pending )
+ {
+ if ( ret == -1 )
+ {
ret = port_info->port;
port_info->pending = 0;
- } else {
+ }
+ else
+ {
files[fd].read = 1;
break;
}
}
}
+
local_irq_restore(flags);
+
return ret;
}
int xenevtchn_unmask(xenevtchn_handle *xce, evtchn_port_t port)
{
unmask_evtchn(port);
+
return 0;
}
int osdep_evtchn_open(xenevtchn_handle *xce)
{
int fd = open(EVTCHN_DEV_NAME, O_NONBLOCK|O_RDWR);
+
if ( fd == -1 )
return -1;
+
xce->fd = fd;
+
return 0;
}
int osdep_evtchn_restrict(xenevtchn_handle *xce, domid_t domid)
{
errno = -EOPNOTSUPP;
+
return -1;
}
return ioctl(fd, IOCTL_EVTCHN_NOTIFY, ¬ify);
}
-xenevtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle * xce, uint32_t domid)
+xenevtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce,
+ uint32_t domid)
{
int fd = xce->fd;
struct ioctl_evtchn_bind_unbound_port bind;
bind.remote_domain = domid;
ret = ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
- if (ret == 0)
+ if ( ret == 0 )
return bind.port;
else
return -1;
}
-xenevtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce, uint32_t domid,
- evtchn_port_t remote_port)
+xenevtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce,
+ uint32_t domid,
+ evtchn_port_t remote_port)
{
int fd = xce->fd;
struct ioctl_evtchn_bind_interdomain bind;
bind.remote_port = remote_port;
ret = ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
- if (ret == 0)
+ if ( ret == 0 )
return bind.port;
else
return -1;
return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
}
-xenevtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce, unsigned int virq)
+xenevtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce,
+ unsigned int virq)
{
int fd = xce->fd;
struct ioctl_evtchn_bind_virq bind;
bind.virq = virq;
err = ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
- if (err)
+ if ( err )
return -1;
else
return bind.port;
int xenevtchn_unmask(xenevtchn_handle *xce, evtchn_port_t port)
{
int fd = xce->fd;
+
return write_exact(fd, (char *)&port, sizeof(port));
}
return ioctl(fd, IOCTL_EVTCHN_NOTIFY, ¬ify);
}
-xenevtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, uint32_t domid)
+xenevtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce,
+ uint32_t domid)
{
int fd = xce->fd;
struct ioctl_evtchn_bind_unbound_port bind;
return ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
}
-xenevtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce, uint32_t domid,
- evtchn_port_t remote_port)
+xenevtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce,
+ uint32_t domid,
+ evtchn_port_t remote_port)
{
int fd = xce->fd;
struct ioctl_evtchn_bind_interdomain bind;
return ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
}
-xenevtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce, unsigned int virq)
+xenevtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce,
+ unsigned int virq)
{
int fd = xce->fd;
struct ioctl_evtchn_bind_virq bind;
int xenevtchn_unmask(xenevtchn_handle *xce, evtchn_port_t port)
{
int fd = xce->fd;
+
return write_exact(fd, (char *)&port, sizeof(port));
}